Skip to content

Commit 7ddc00c

Browse files
Initial port of the new language service work.
1 parent 2775fc2 commit 7ddc00c

File tree

9 files changed

+880
-782
lines changed

9 files changed

+880
-782
lines changed

src/harness/fourslash.ts

Lines changed: 77 additions & 101 deletions
Large diffs are not rendered by default.

src/harness/harnessLanguageService.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ module Harness.LanguageService {
281281
}
282282

283283
/** Verify that applying edits to sourceFileName result in the content of the file baselineFileName */
284-
public checkEdits(sourceFileName: string, baselineFileName: string, edits: ts.TextEdit[]) {
284+
public checkEdits(sourceFileName: string, baselineFileName: string, edits: ts.TextChange[]) {
285285
var script = Harness.IO.readFile(sourceFileName);
286286
var formattedScript = this.applyEdits(script, edits);
287287
var baseline = Harness.IO.readFile(baselineFileName);
@@ -310,34 +310,34 @@ module Harness.LanguageService {
310310

311311

312312
/** Apply an array of text edits to a string, and return the resulting string. */
313-
public applyEdits(content: string, edits: ts.TextEdit[]): string {
313+
public applyEdits(content: string, edits: ts.TextChange[]): string {
314314
var result = content;
315315
edits = this.normalizeEdits(edits);
316316

317317
for (var i = edits.length - 1; i >= 0; i--) {
318318
var edit = edits[i];
319-
var prefix = result.substring(0, edit.minChar);
320-
var middle = edit.text;
321-
var suffix = result.substring(edit.limChar);
319+
var prefix = result.substring(0, edit.span.start());
320+
var middle = edit.newText;
321+
var suffix = result.substring(edit.span.end());
322322
result = prefix + middle + suffix;
323323
}
324324
return result;
325325
}
326326

327327
/** Normalize an array of edits by removing overlapping entries and sorting entries on the minChar position. */
328-
private normalizeEdits(edits: ts.TextEdit[]): ts.TextEdit[] {
329-
var result: ts.TextEdit[] = [];
328+
private normalizeEdits(edits: ts.TextChange[]): ts.TextChange[] {
329+
var result: ts.TextChange[] = [];
330330

331-
function mapEdits(edits: ts.TextEdit[]): { edit: ts.TextEdit; index: number; }[] {
332-
var result: { edit: ts.TextEdit; index: number; }[] = [];
331+
function mapEdits(edits: ts.TextChange[]): { edit: ts.TextChange; index: number; }[] {
332+
var result: { edit: ts.TextChange; index: number; }[] = [];
333333
for (var i = 0; i < edits.length; i++) {
334334
result.push({ edit: edits[i], index: i });
335335
}
336336
return result;
337337
}
338338

339339
var temp = mapEdits(edits).sort(function (a, b) {
340-
var result = a.edit.minChar - b.edit.limChar;
340+
var result = a.edit.span.start() - b.edit.span.start();
341341
if (result === 0)
342342
result = a.index - b.index;
343343
return result;
@@ -356,7 +356,7 @@ module Harness.LanguageService {
356356
}
357357
var nextEdit = temp[next].edit;
358358

359-
var gap = nextEdit.minChar - currentEdit.limChar;
359+
var gap = nextEdit.span.start() - currentEdit.span.end();
360360

361361
// non-overlapping edits
362362
if (gap >= 0) {
@@ -365,10 +365,10 @@ module Harness.LanguageService {
365365
next++;
366366
continue;
367367
}
368-
368+
369369
// overlapping edits: for now, we only support ignoring an next edit
370370
// entirely contained in the current edit.
371-
if (currentEdit.minChar >= nextEdit.limChar) {
371+
if (currentEdit.span.end() >= nextEdit.span.end()) {
372372
next++;
373373
continue;
374374
}

src/services/braceMatcher.ts

Lines changed: 29 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -24,93 +24,50 @@ module TypeScript.Services {
2424
public static getMatchSpans(syntaxTree: TypeScript.SyntaxTree, position: number): TypeScript.TextSpan[] {
2525
var result: TypeScript.TextSpan[] = [];
2626

27-
var currentToken = findToken(syntaxTree.sourceUnit(), position);
27+
var token = findToken(syntaxTree.sourceUnit(), position);
2828

29-
BraceMatcher.getMatchingCloseBrace(currentToken, position, result);
30-
BraceMatcher.getMatchingOpenBrace(currentToken, position, result);
29+
if (start(token) === position) {
30+
var matchKind = BraceMatcher.getMatchingTokenKind(token);
3131

32-
return result;
33-
}
32+
if (matchKind !== null) {
33+
var parentElement = token.parent;
3434

35-
private static getMatchingCloseBrace(currentToken: TypeScript.ISyntaxToken, position: number, result: TypeScript.TextSpan[]) {
36-
if (start(currentToken) === position) {
37-
var closingBraceKind = BraceMatcher.getMatchingCloseBraceTokenKind(currentToken);
38-
if (closingBraceKind !== null) {
39-
var parentElement = currentToken.parent;
40-
var currentPosition = fullStart(currentToken.parent);
4135
for (var i = 0, n = childCount(parentElement); i < n; i++) {
42-
var element = childAt(parentElement, i);
43-
if (element !== null && fullWidth(element) > 0) {
44-
if (element.kind() === closingBraceKind) {
45-
var range1 = new TypeScript.TextSpan(position, width(currentToken));
46-
var range2 = new TypeScript.TextSpan(currentPosition + leadingTriviaWidth(element), width(element));
47-
result.push(range1, range2);
48-
break;
49-
}
50-
51-
currentPosition += fullWidth(element);
52-
}
53-
}
54-
}
55-
}
56-
}
57-
58-
private static getMatchingOpenBrace(currentToken: TypeScript.ISyntaxToken, position: number, result: TypeScript.TextSpan[]) {
59-
// Check if the current token to the left is a close brace
60-
if (currentToken.fullStart() === position) {
61-
currentToken = previousToken(currentToken);
62-
}
36+
var current = childAt(parentElement, i);
6337

64-
if (currentToken !== null && start(currentToken) === (position - 1)) {
65-
var openBraceKind = BraceMatcher.getMatchingOpenBraceTokenKind(currentToken);
66-
if (openBraceKind !== null) {
67-
var parentElement = currentToken.parent;
68-
var currentPosition = fullStart(currentToken.parent) + fullWidth(parentElement);
69-
for (var i = childCount(parentElement) - 1 ; i >= 0; i--) {
70-
var element = childAt(parentElement, i);
71-
if (element !== null && fullWidth(element) > 0) {
72-
if (element.kind() === openBraceKind) {
73-
var range1 = new TypeScript.TextSpan(position - 1, width(currentToken));
74-
var range2 = new TypeScript.TextSpan(currentPosition - lastToken(element).trailingTriviaWidth() - width(element), width(element));
75-
result.push(range1, range2);
38+
if (current !== null && fullWidth(current) > 0) {
39+
if (current.kind() === matchKind) {
40+
var range1 = new TypeScript.TextSpan(start(token), width(token));
41+
var range2 = new TypeScript.TextSpan(start(current), width(current));
42+
if (range1.start() < range2.start()) {
43+
result.push(range1, range2);
44+
}
45+
else {
46+
result.push(range2, range1);
47+
}
7648
break;
7749
}
78-
79-
currentPosition -= fullWidth(element);
8050
}
8151
}
8252
}
8353
}
84-
}
8554

86-
private static getMatchingCloseBraceTokenKind(positionedElement: TypeScript.ISyntaxElement): TypeScript.SyntaxKind {
87-
var element = positionedElement !== null && positionedElement;
88-
switch (element.kind()) {
89-
case TypeScript.SyntaxKind.OpenBraceToken:
90-
return TypeScript.SyntaxKind.CloseBraceToken;
91-
case TypeScript.SyntaxKind.OpenParenToken:
92-
return TypeScript.SyntaxKind.CloseParenToken;
93-
case TypeScript.SyntaxKind.OpenBracketToken:
94-
return TypeScript.SyntaxKind.CloseBracketToken;
95-
case TypeScript.SyntaxKind.LessThanToken:
96-
return TypeScript.SyntaxUtilities.isAngleBracket(positionedElement) ? TypeScript.SyntaxKind.GreaterThanToken : null;
97-
}
98-
return null;
55+
return result;
9956
}
10057

101-
private static getMatchingOpenBraceTokenKind(positionedElement: TypeScript.ISyntaxElement): TypeScript.SyntaxKind {
102-
var element = positionedElement !== null && positionedElement;
103-
switch (element.kind()) {
104-
case TypeScript.SyntaxKind.CloseBraceToken:
105-
return TypeScript.SyntaxKind.OpenBraceToken
106-
case TypeScript.SyntaxKind.CloseParenToken:
107-
return TypeScript.SyntaxKind.OpenParenToken;
108-
case TypeScript.SyntaxKind.CloseBracketToken:
109-
return TypeScript.SyntaxKind.OpenBracketToken;
110-
case TypeScript.SyntaxKind.GreaterThanToken:
111-
return TypeScript.SyntaxUtilities.isAngleBracket(positionedElement) ? TypeScript.SyntaxKind.LessThanToken : null;
58+
private static getMatchingTokenKind(token: TypeScript.ISyntaxToken): TypeScript.SyntaxKind {
59+
switch (token.kind()) {
60+
case TypeScript.SyntaxKind.OpenBraceToken: return TypeScript.SyntaxKind.CloseBraceToken
61+
case TypeScript.SyntaxKind.OpenParenToken: return TypeScript.SyntaxKind.CloseParenToken;
62+
case TypeScript.SyntaxKind.OpenBracketToken: return TypeScript.SyntaxKind.CloseBracketToken;
63+
case TypeScript.SyntaxKind.LessThanToken: return TypeScript.SyntaxKind.GreaterThanToken;
64+
case TypeScript.SyntaxKind.CloseBraceToken: return TypeScript.SyntaxKind.OpenBraceToken
65+
case TypeScript.SyntaxKind.CloseParenToken: return TypeScript.SyntaxKind.OpenParenToken;
66+
case TypeScript.SyntaxKind.CloseBracketToken: return TypeScript.SyntaxKind.OpenBracketToken;
67+
case TypeScript.SyntaxKind.GreaterThanToken: return TypeScript.SyntaxKind.LessThanToken;
11268
}
69+
11370
return null;
11471
}
11572
}
116-
}
73+
}

0 commit comments

Comments
 (0)