Skip to content

Commit

Permalink
improve code complete feature with history words
Browse files Browse the repository at this point in the history
  • Loading branch information
wudixiaop committed Oct 17, 2015
1 parent 77dfde3 commit 4fda4ce
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Src/ShaderlabVS/License.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright © 2014 Rocky Lai
Copyright © 2014 - 2015 Rocky Lai

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the “Software”), to deal in the Software without restriction,
Expand Down
109 changes: 77 additions & 32 deletions Src/ShaderlabVS/ShaderlabCodeCompletion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Windows.Media;
using System.Windows.Media.Imaging;

Expand All @@ -22,7 +23,7 @@ namespace ShaderlabVS
#region Shaderlab Completion Source
public class ShaderlabCompletionSource : ICompletionSource
{

private static HashSet<string> WordsInDocuments = new HashSet<string>();
private ShaderlabCompletionSourceProvider sourceProvider;
private ITextBuffer textBuffer;

Expand All @@ -32,6 +33,34 @@ public ShaderlabCompletionSource(ShaderlabCompletionSourceProvider completonSour
this.textBuffer = textBuffer;
}

public static void SetWordsInDocuments(string text)
{
StringReader reader = new StringReader(text);

string line = reader.ReadLine();

while (line != null)
{
if (Utilities.IsCommentLine(line))
{
line = reader.ReadLine();
continue;
}

string[] words = line.Split(
new char[] { '{', '}', ' ', '\t', '(', ')', '[', ']', '+', '-', '*', '/', '%', '^', '>', '<', ':',
'.', ';', '\"', '\'', '?', '\\', '&', '|', '`', '$', '#', ','},
StringSplitOptions.RemoveEmptyEntries);

foreach (var word in words)
{
WordsInDocuments.Add(word);
}

line = reader.ReadLine();
}
}

private ITrackingSpan FindTokenSpanAtPosition(ITrackingPoint point, ICompletionSession completionSession)
{
SnapshotPoint ssPoint = (completionSession.TextView.Caret.Position.BufferPosition) - 1;
Expand All @@ -44,68 +73,73 @@ private ITrackingSpan FindTokenSpanAtPosition(ITrackingPoint point, ICompletionS
public void AugmentCompletionSession(ICompletionSession session, IList<CompletionSet> completionSets)
{
List<Completion> completionList = new List<Completion>();
ImageSource functionsImage = GetImageFromAssetByName("Method.png");
ImageSource datatypeImage = GetImageFromAssetByName("Structure.png");
ImageSource keywordsImage = GetImageFromAssetByName("Keywords.png");
ImageSource valuesImage = GetImageFromAssetByName("Values.png");

HashSet<string> keywords = new HashSet<string>();

// Add functions into auto completion list
//
ImageSource functionsImage = GetImageFromAssetByName("Method.png");
ShaderlabDataManager.Instance.HLSLCGFunctions.ForEach(f =>
{
completionList.Add(new Completion(f.Name, f.Name, f.Description, functionsImage, null));
keywords.Add(f.Name);
});

ShaderlabDataManager.Instance.UnityBuiltinFunctions.ForEach(f =>
{
completionList.Add(new Completion(f.Name, f.Name, f.Description, functionsImage, null));
});


{
completionList.Add(new Completion(f.Name, f.Name, f.Description, functionsImage, null));
keywords.Add(f.Name);
});

// Datatypes
//
ImageSource datatypeImage = GetImageFromAssetByName("Structure.png");

ShaderlabDataManager.Instance.HLSLCGDatatypes.ForEach(d =>
{
completionList.Add(new Completion(d, d, "", datatypeImage, null));
});
{
completionList.Add(new Completion(d, d, "", datatypeImage, null));
keywords.Add(d);
});

ShaderlabDataManager.Instance.UnityBuiltinDatatypes.ForEach(d =>
{
completionList.Add(new Completion(d.Name, d.Name, d.Description, datatypeImage, null));
});
{
completionList.Add(new Completion(d.Name, d.Name, d.Description, datatypeImage, null));
keywords.Add(d.Name);
});

// Keywords
//
ImageSource keywordsImage = GetImageFromAssetByName("Keywords.png");

ShaderlabDataManager.Instance.HLSLCGBlockKeywords.ForEach(k =>
{
completionList.Add(new Completion(k, k, "", keywordsImage, null));
});
{
completionList.Add(new Completion(k, k, "", keywordsImage, null));
keywords.Add(k);
});

ShaderlabDataManager.Instance.HLSLCGNonblockKeywords.ForEach(k =>
{
completionList.Add(new Completion(k, k, "", keywordsImage, null));
});
{
completionList.Add(new Completion(k, k, "", keywordsImage, null));
keywords.Add(k);
});

ShaderlabDataManager.Instance.HLSLCGSpecialKeywords.ForEach(k =>
{
completionList.Add(new Completion(k, k, "", keywordsImage, null));
keywords.Add(k);
});

ShaderlabDataManager.Instance.UnityKeywords.ForEach(k =>
{
completionList.Add(new Completion(k.Name, k.Name, k.Description, keywordsImage, null));
});
{
completionList.Add(new Completion(k.Name, k.Name, k.Description, keywordsImage, null));
keywords.Add(k.Name);
});

// values/enums
ImageSource valuesImage = GetImageFromAssetByName("Values.png");

ShaderlabDataManager.Instance.UnityBuiltinValues.ForEach(v =>
{
completionList.Add(new Completion(v.Name, v.Name, v.VauleDescription, valuesImage, null));
});

{
completionList.Add(new Completion(v.Name, v.Name, v.VauleDescription, valuesImage, null));
keywords.Add(v.Name);
});

// Macros
//
Expand All @@ -120,8 +154,19 @@ public void AugmentCompletionSession(ICompletionSession session, IList<Completio
{
completionList.Add(new Completion(m.Name, m.Name, description, valuesImage, null));
}
keywords.Add(m.Name);
});

// Add words in current file
//
foreach (var word in WordsInDocuments)
{
if (!keywords.Contains(word))
{
completionList.Add(new Completion(word, word, string.Empty, valuesImage, null));
}
}

completionSets.Add(new CompletionSet("Token", "Token", FindTokenSpanAtPosition(session.GetTriggerPoint(this.textBuffer), session), completionList, null));
}

Expand Down
4 changes: 3 additions & 1 deletion Src/ShaderlabVS/ShaderlabVS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,13 @@ public ShaderlabClassifier(ITextBuffer buffer, IClassificationTypeRegistryServic

public IEnumerable<ITagSpan<ClassificationTag>> GetTags(NormalizedSnapshotSpanCollection spans)
{
ShaderlabCompletionSource.SetWordsInDocuments(spans[0].Snapshot.GetText());

string text = " " + spans[0].Snapshot.GetText().ToLower();
scanner.SetSource(text, 0);
int token;
IClassificationType cf;

do
{
token = scanner.NextToken();
Expand Down
1 change: 1 addition & 0 deletions Src/ShaderlabVS/ShaderlabVS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CopyVsixExtensionFiles>False</CopyVsixExtensionFiles>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand Down
2 changes: 1 addition & 1 deletion Src/ShaderlabVS/source.extension.vsixmanifest
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="ShaderlabVS..c702cfb7-573c-45f4-9469-115fcb519ad2" Version="0.6" Language="zh-Hans" Publisher="Rocky Lai" />
<Identity Id="ShaderlabVS..c702cfb7-573c-45f4-9469-115fcb519ad2" Version="0.6" Language="zh-CN" Publisher="Rocky Lai" />
<DisplayName>ShaderlabVS</DisplayName>
<Description xml:space="preserve">ShaderlabVS is a Visual Studio plugin for Unity3D shader programming.</Description>
<MoreInfo>https://github.com/wudixiaop/ShaderlabVS/blob/master/README.md</MoreInfo>
Expand Down

0 comments on commit 4fda4ce

Please sign in to comment.