From 42c1f0c5e4828bfb5cf98cd423de88fc5abc6aa6 Mon Sep 17 00:00:00 2001 From: Adeel Date: Mon, 20 Jan 2014 00:31:44 +0300 Subject: [PATCH] Outlining: Fixes for TypeScript. --- .../JavaScript/JavaScriptArrayOutlining.cs | 2 +- .../JavaScript/JavaScriptOutlining.cs | 31 +++++++++++-------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/EditorExtensions/Classifications/JavaScript/JavaScriptArrayOutlining.cs b/EditorExtensions/Classifications/JavaScript/JavaScriptArrayOutlining.cs index 1c2f36f29..faab5b395 100644 --- a/EditorExtensions/Classifications/JavaScript/JavaScriptArrayOutlining.cs +++ b/EditorExtensions/Classifications/JavaScript/JavaScriptArrayOutlining.cs @@ -58,7 +58,7 @@ public IEnumerable> 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); diff --git a/EditorExtensions/Classifications/JavaScript/JavaScriptOutlining.cs b/EditorExtensions/Classifications/JavaScript/JavaScriptOutlining.cs index 14360c4bf..ebc71a53d 100644 --- a/EditorExtensions/Classifications/JavaScript/JavaScriptOutlining.cs +++ b/EditorExtensions/Classifications/JavaScript/JavaScriptOutlining.cs @@ -29,6 +29,11 @@ internal sealed class JavaScriptOutliningTagger : ITagger ITextBuffer buffer; ITextSnapshot snapshot; List 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) { @@ -56,10 +61,9 @@ public IEnumerable> 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); @@ -70,21 +74,22 @@ public IEnumerable> 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>(jsTaggerType); - - var classifications = tagger.GetTags(new NormalizedSnapshotSpanCollection(start.Extent)); + try + { + var tagger = start.Snapshot.TextBuffer.Properties.GetProperty>(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 TagsChanged;