Skip to content
This repository has been archived by the owner on Oct 4, 2021. It is now read-only.

Commit

Permalink
Merge pull request #5127 from mono/backport-pr-5102-to-release-7.6
Browse files Browse the repository at this point in the history
[release-7.6] Fix #4822: Fixing code comment command for all cases
  • Loading branch information
slluis committed Jun 21, 2018
2 parents 9743981 + 78ede05 commit 0069905
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
Expand Up @@ -50,6 +50,9 @@ internal sealed class TagBasedSyntaxHighlighting : ISyntaxHighlighting
private ITextView textView { get; }
private IAccurateClassifier classifier { get; set; }
readonly Dictionary<string, ScopeStack> classificationMap;
Dictionary<IClassificationType, ScopeStack> classificationTypeToScopeCache = new Dictionary<IClassificationType, ScopeStack> ();
ScopeStack defaultScopeStack = new ScopeStack (EditorThemeColors.Foreground);
ScopeStack userScope;
private MonoDevelop.Ide.Editor.ITextDocument textDocument { get; }

internal TagBasedSyntaxHighlighting (ITextView textView, string defaultScope)
Expand All @@ -58,6 +61,7 @@ internal TagBasedSyntaxHighlighting (ITextView textView, string defaultScope)
this.textDocument = textView.GetTextEditor ();
if (defaultScope != null)
classificationMap = GetClassificationMap (defaultScope);
this.userScope = this.defaultScopeStack.Push (EditorThemeColors.UserTypes);
}

public Task<HighlightedLine> GetHighlightedLineAsync (IDocumentLine line, CancellationToken cancellationToken)
Expand All @@ -78,7 +82,7 @@ public Task<HighlightedLine> GetHighlightedLineAsync (IDocumentLine line, Cancel
ScopeStack scopeStack;
foreach (ClassificationSpan curSpan in classifications) {
if (curSpan.Span.Start > lastClassifiedOffsetEnd) {
scopeStack = new ScopeStack (EditorThemeColors.Foreground);
scopeStack = userScope;
ColoredSegment whitespaceSegment = new ColoredSegment (lastClassifiedOffsetEnd - start, curSpan.Span.Start - lastClassifiedOffsetEnd, scopeStack);
coloredSegments.Add (whitespaceSegment);
}
Expand All @@ -95,7 +99,7 @@ public Task<HighlightedLine> GetHighlightedLineAsync (IDocumentLine line, Cancel
}

if (end > lastClassifiedOffsetEnd) {
scopeStack = new ScopeStack (EditorThemeColors.Foreground);
scopeStack = userScope;
ColoredSegment whitespaceSegment = new ColoredSegment (lastClassifiedOffsetEnd - start, end - lastClassifiedOffsetEnd, scopeStack);
coloredSegments.Add (whitespaceSegment);
}
Expand Down Expand Up @@ -162,7 +166,7 @@ public async Task<ScopeStack> GetScopeStackAsync (int offset, CancellationToken
if (segment.Offset <= offset && segment.EndOffset >= offset)
return segment.ScopeStack;
}
return ScopeStack.Empty;
return defaultScopeStack;
}

private EventHandler<LineEventArgs> _highlightingStateChanged;
Expand Down Expand Up @@ -216,9 +220,6 @@ private void OnClassificationChanged (object sender, ClassificationChangedEventA
}
}

Dictionary<IClassificationType, ScopeStack> classificationTypeToScopeCache = new Dictionary<IClassificationType, ScopeStack> ();
static ScopeStack defaultScopeStack = new ScopeStack (EditorThemeColors.Foreground);

private ScopeStack GetScopeStackFromClassificationType (IClassificationType classificationType)
{
if (classificationTypeToScopeCache.TryGetValue (classificationType, out var cachedScope))
Expand Down Expand Up @@ -387,12 +388,12 @@ static ScopeStack MakeScope (ScopeStack defaultScope, string scope)
}

static ImmutableDictionary<string, Dictionary<string, ScopeStack>> classificationMapCache = ImmutableDictionary<string, Dictionary<string, ScopeStack>>.Empty;
static Dictionary<string, ScopeStack> GetClassificationMap (string scope)
Dictionary<string, ScopeStack> GetClassificationMap (string scope)
{
Dictionary<string, ScopeStack> result;
defaultScopeStack = new ScopeStack (scope);
if (classificationMapCache.TryGetValue (scope, out result))
return result;
var defaultScopeStack = new ScopeStack (scope);
result = new Dictionary<string, ScopeStack> {
[ClassificationTypeNames.Comment] = MakeScope (defaultScopeStack, "comment." + scope),
[ClassificationTypeNames.ExcludedCode] = MakeScope (defaultScopeStack, "comment.excluded." + scope),
Expand Down
Expand Up @@ -211,6 +211,36 @@ void Bar ()
}
}



[Test]
public async Task TestToggle_Visible_StartOfLine ()
{
//We need to create full document and not just editor
//so extensions are initialized which set custom C#
//tagger based syntax highligthing
const string input = @"class Foo
{
void Bar ()
$ {
//test
}
}";
using (var testCase = await SetupTestCase ("", wrap: true)) {
var editor = testCase.Document.Editor;
SetupInput (editor, input);

//Call UpdateParseDocument so AdHock Roslyn Workspace is created for file
await testCase.Document.UpdateParseDocument ();

//Finnaly call command Update so it sets values which we assert
var info = new Components.Commands.CommandInfo ();
testCase.GetContent<DefaultCommandTextEditorExtension> ().OnUpdateToggleComment (info);
Assert.AreEqual (true, info.Visible);
Assert.AreEqual (true, info.Enabled);
}
}

[Test]
public async Task TestToggle_AddAsync ()
{
Expand Down

0 comments on commit 0069905

Please sign in to comment.