Skip to content

Commit

Permalink
Outlining: Fixes for TypeScript.
Browse files Browse the repository at this point in the history
  • Loading branch information
am11 committed Jan 19, 2014
1 parent ac3ed7c commit 42c1f0c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public IEnumerable<ITagSpan<IOutliningRegionTag>> GetTags(NormalizedSnapshotSpan
var startLine = currentSnapshot.GetLineFromLineNumber(region.StartLine);
string lineText = startLine.GetText().Trim();

if (!JavaScriptOutliningTagger.HasReservedBlockKeywords(startLine, new[] { "function" }))
if (!JavaScriptOutliningTagger.HasReservedBlockKeywords(startLine, "function"))
{
var endLine = currentSnapshot.GetLineFromLineNumber(region.EndLine);
var contentSpan = new SnapshotSpan(startLine.Start + region.StartOffset, endLine.End);
Expand Down
31 changes: 18 additions & 13 deletions EditorExtensions/Classifications/JavaScript/JavaScriptOutlining.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ internal sealed class JavaScriptOutliningTagger : ITagger<IOutliningRegionTag>
ITextBuffer buffer;
ITextSnapshot snapshot;
List<Region> regions;
static readonly Type jsTaggerType = typeof(Microsoft.VisualStudio.JSLS.JavaScriptLanguageService).Assembly.GetType("Microsoft.VisualStudio.JSLS.Classification.Tagger");
static char[] PunctuationCharacters = Enumerable.Range(1, char.MaxValue)
.Where(x => !(char.IsLetterOrDigit((char)x)))
.Select(i => (char)i)
.ToArray();

public JavaScriptOutliningTagger(ITextBuffer buffer)
{
Expand Down Expand Up @@ -56,10 +61,9 @@ public IEnumerable<ITagSpan<IOutliningRegionTag>> GetTags(NormalizedSnapshotSpan
if (region.StartLine <= endLineNumber && region.EndLine >= startLineNumber)
{
var startLine = currentSnapshot.GetLineFromLineNumber(region.StartLine);
string lineText = startLine.GetText().Trim();

// Note: if we revoke this condition, clicking (+) button 'twice' would expand the region while Ctrl+M,M works as expected.
if (!HasReservedBlockKeywords(startLine, new[] { "function", "module", "constructor", "void", "class" }))
if (!HasReservedBlockKeywords(startLine, "function", "module", "constructor", "void", "class"))
{
var endLine = currentSnapshot.GetLineFromLineNumber(region.EndLine);
var contentSpan = new SnapshotSpan(startLine.Start + region.StartOffset, endLine.End);
Expand All @@ -70,21 +74,22 @@ public IEnumerable<ITagSpan<IOutliningRegionTag>> GetTags(NormalizedSnapshotSpan
}
}

static readonly Type jsTaggerType = typeof(Microsoft.VisualStudio.JSLS.JavaScriptLanguageService).Assembly.GetType("Microsoft.VisualStudio.JSLS.Classification.Tagger");

public static bool HasReservedBlockKeywords(ITextSnapshotLine start, string[] nativelySupportedTokens)
public static bool HasReservedBlockKeywords(ITextSnapshotLine start, params string[] nativelySupportedTokens)
{
var tagger = start.Snapshot.TextBuffer.Properties.GetProperty<ITagger<ClassificationTag>>(jsTaggerType);

var classifications = tagger.GetTags(new NormalizedSnapshotSpanCollection(start.Extent));
try
{
var tagger = start.Snapshot.TextBuffer.Properties.GetProperty<ITagger<ClassificationTag>>(jsTaggerType);
var classifications = tagger.GetTags(new NormalizedSnapshotSpanCollection(start.Extent));

foreach (var tag in classifications.Where(c => !c.Tag.ClassificationType.IsOfType("comment")))
return classifications.Any(tag => !tag.Tag.ClassificationType.IsOfType("comment") &&
tag.Tag.ClassificationType.IsOfType("keyword") &&
nativelySupportedTokens.Contains(tag.Span.GetText()));
}
catch
{
if (tag.Tag.ClassificationType.IsOfType("keyword") && nativelySupportedTokens.Contains(tag.Span.GetText()))
return true;
return start.GetText().Trim().Split(PunctuationCharacters)
.Any(word => nativelySupportedTokens.Contains(word));
}

return false;
}

public event EventHandler<SnapshotSpanEventArgs> TagsChanged;
Expand Down

0 comments on commit 42c1f0c

Please sign in to comment.