Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

Commit de23788

Browse files
author
N. Taylor Mullen
committed
Move indentation service to VS.Editor.Razor and export it.
- Removed indentation service extensions and made `GetDesiredIndentation` understand `ITextSnapshot`s and `ITextLineSnapshot`. - Updated our test `StringTextSnapshot` implementation to have the required features our indentation tests require. - Updated indentation service tests to use new API. #1762
1 parent 44a4718 commit de23788

File tree

6 files changed

+96
-127
lines changed

6 files changed

+96
-127
lines changed

src/Microsoft.CodeAnalysis.Razor.Workspaces/DefaultRazorIndentationFactsServiceFactory.cs

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/Microsoft.CodeAnalysis.Razor.Workspaces/DefaultRazorIndentationFactsService.cs renamed to src/Microsoft.VisualStudio.Editor.Razor/DefaultRazorIndentationFactsService.cs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,38 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.ComponentModel.Composition;
67
using Microsoft.AspNetCore.Razor.Language;
78
using Microsoft.AspNetCore.Razor.Language.Legacy;
9+
using Microsoft.VisualStudio.Text;
10+
using Span = Microsoft.AspNetCore.Razor.Language.Legacy.Span;
811

9-
namespace Microsoft.CodeAnalysis.Razor
12+
namespace Microsoft.VisualStudio.Editor.Razor
1013
{
14+
[System.Composition.Shared]
15+
[Export(typeof(RazorIndentationFactsService))]
1116
internal class DefaultRazorIndentationFactsService : RazorIndentationFactsService
1217
{
13-
public override int? GetDesiredIndentation(RazorSyntaxTree syntaxTree, int previousLineEndIndex, Func<int, string> getLineContent, int indentSize, int tabSize)
18+
public override int? GetDesiredIndentation(
19+
RazorSyntaxTree syntaxTree,
20+
ITextSnapshot syntaxTreeSnapshot,
21+
ITextSnapshotLine line,
22+
int indentSize,
23+
int tabSize)
1424
{
1525
if (syntaxTree == null)
1626
{
1727
throw new ArgumentNullException(nameof(syntaxTree));
1828
}
1929

20-
if (previousLineEndIndex < 0)
30+
if (syntaxTreeSnapshot == null)
2131
{
22-
throw new ArgumentOutOfRangeException(nameof(previousLineEndIndex));
32+
throw new ArgumentNullException(nameof(syntaxTreeSnapshot));
2333
}
2434

25-
if (getLineContent == null)
35+
if (line == null)
2636
{
27-
throw new ArgumentNullException(nameof(getLineContent));
37+
throw new ArgumentNullException(nameof(line));
2838
}
2939

3040
if (indentSize < 0)
@@ -37,6 +47,10 @@ internal class DefaultRazorIndentationFactsService : RazorIndentationFactsServic
3747
throw new ArgumentOutOfRangeException(nameof(tabSize));
3848
}
3949

50+
var previousLine = line.Snapshot.GetLineFromLineNumber(line.LineNumber - 1);
51+
var trackingPoint = previousLine.Snapshot.CreateTrackingPoint(previousLine.End, PointTrackingMode.Negative);
52+
var previousLineEndIndex = trackingPoint.GetPosition(syntaxTreeSnapshot);
53+
4054
var simulatedChange = new SourceChange(previousLineEndIndex, 0, string.Empty);
4155
var owningSpan = LocateOwner(syntaxTree.Root, simulatedChange);
4256
if (owningSpan.Kind == SpanKindInternal.Code)
@@ -80,8 +94,8 @@ currentSpan.Symbols[0] is CSharpSymbol symbol &&
8094
// We can't rely on the syntax trees representation of the source document because partial parses may have mutated
8195
// the underlying SyntaxTree text buffer. Because of this, if we want to provide accurate indentations we need to
8296
// operate on the current line representation as indicated by the provider.
83-
var line = getLineContent(currentSpan.Start.LineIndex);
84-
desiredIndentation = GetIndentLevelOfLine(line, tabSize) + indentSize;
97+
var lineText = line.Snapshot.GetLineFromLineNumber(currentSpan.Start.LineIndex).GetText();
98+
desiredIndentation = GetIndentLevelOfLine(lineText, tabSize) + indentSize;
8599
}
86100
}
87101

src/Microsoft.CodeAnalysis.Razor.Workspaces/RazorIndentationFactsService.cs renamed to src/Microsoft.VisualStudio.Editor.Razor/RazorIndentationFactsService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
using System;
54
using Microsoft.AspNetCore.Razor.Language;
65
using Microsoft.CodeAnalysis.Host;
6+
using Microsoft.VisualStudio.Text;
77

8-
namespace Microsoft.CodeAnalysis.Razor
8+
namespace Microsoft.VisualStudio.Editor.Razor
99
{
1010
public abstract class RazorIndentationFactsService : ILanguageService
1111
{
12-
public abstract int? GetDesiredIndentation(RazorSyntaxTree syntaxTree, int previousLineEnd, Func<int, string> lineProvider, int indentSize, int tabSize);
12+
public abstract int? GetDesiredIndentation(RazorSyntaxTree syntaxTree, ITextSnapshot syntaxTreeSnapshot, ITextSnapshotLine line, int indentSize, int tabSize);
1313
}
1414
}

src/Microsoft.VisualStudio.Editor.Razor/RazorIndentationFactsServiceExtensions.cs

Lines changed: 0 additions & 30 deletions
This file was deleted.

test/Microsoft.VisualStudio.Editor.Razor.Test.Common/StringTextSnapshot.cs

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,20 @@ public ITextSnapshotLine GetLineFromPosition(int position)
7777
return matchingLine;
7878
}
7979

80-
public ITrackingPoint CreateTrackingPoint(int position, PointTrackingMode trackingMode) => throw new NotImplementedException();
80+
public ITextSnapshotLine GetLineFromLineNumber(int lineNumber)
81+
{
82+
if (lineNumber < 0 || lineNumber >= _lines.Count)
83+
{
84+
throw new ArgumentOutOfRangeException(nameof(lineNumber));
85+
}
86+
87+
return _lines[lineNumber];
88+
}
89+
90+
public ITrackingPoint CreateTrackingPoint(int position, PointTrackingMode trackingMode)
91+
{
92+
return new SnapshotTrackingPoint(position);
93+
}
8194

8295
public ITrackingPoint CreateTrackingPoint(int position, PointTrackingMode trackingMode, TrackingFidelityMode trackingFidelity) => throw new NotImplementedException();
8396

@@ -89,8 +102,6 @@ public ITextSnapshotLine GetLineFromPosition(int position)
89102

90103
public ITrackingSpan CreateTrackingSpan(int start, int length, SpanTrackingMode trackingMode, TrackingFidelityMode trackingFidelity) => throw new NotImplementedException();
91104

92-
public ITextSnapshotLine GetLineFromLineNumber(int lineNumber) => throw new NotImplementedException();
93-
94105
public int GetLineNumberFromPosition(int position) => throw new NotImplementedException();
95106

96107
public string GetText(VisualStudio.Text.Span span) => throw new NotImplementedException();
@@ -133,6 +144,30 @@ private class TextChangeCollection : List<ITextChange>, INormalizedTextChangeCol
133144
}
134145
}
135146

147+
private class SnapshotTrackingPoint : ITrackingPoint
148+
{
149+
private readonly int _position;
150+
151+
public SnapshotTrackingPoint(int position)
152+
{
153+
_position = position;
154+
}
155+
156+
public ITextBuffer TextBuffer => throw new NotImplementedException();
157+
158+
public PointTrackingMode TrackingMode => throw new NotImplementedException();
159+
160+
public TrackingFidelityMode TrackingFidelity => throw new NotImplementedException();
161+
162+
public char GetCharacter(ITextSnapshot snapshot) => throw new NotImplementedException();
163+
164+
public SnapshotPoint GetPoint(ITextSnapshot snapshot) => throw new NotImplementedException();
165+
166+
public int GetPosition(ITextSnapshot snapshot) => _position;
167+
168+
public int GetPosition(ITextVersion version) => throw new NotImplementedException();
169+
}
170+
136171
private class SnapshotLine : ITextSnapshotLine
137172
{
138173
private readonly string _contentWithLineBreak;
@@ -153,7 +188,9 @@ public SnapshotLine(string contentWithLineBreak, int start, ITextSnapshot owner)
153188
}
154189

155190
Start = new SnapshotPoint(owner, start);
191+
End = new SnapshotPoint(owner, start + _content.Length);
156192
Snapshot = owner;
193+
LineNumber = (owner as StringTextSnapshot)._lines.Count;
157194
}
158195

159196
public ITextSnapshot Snapshot { get; }
@@ -172,14 +209,14 @@ public SnapshotLine(string contentWithLineBreak, int start, ITextSnapshot owner)
172209

173210
public string GetTextIncludingLineBreak() => _contentWithLineBreak;
174211

175-
public int LineNumber => throw new NotImplementedException();
212+
public int LineNumber { get; }
213+
214+
public SnapshotPoint End { get; }
176215

177216
public SnapshotSpan Extent => throw new NotImplementedException();
178217

179218
public SnapshotSpan ExtentIncludingLineBreak => throw new NotImplementedException();
180219

181-
public SnapshotPoint End => throw new NotImplementedException();
182-
183220
public SnapshotPoint EndIncludingLineBreak => throw new NotImplementedException();
184221
}
185222
}

0 commit comments

Comments
 (0)