Skip to content

Commit dcc3e72

Browse files
author
Andy
authored
Use decodeSpan more (microsoft#16990)
1 parent ee48c1b commit dcc3e72

1 file changed

Lines changed: 82 additions & 149 deletions

File tree

src/server/client.ts

Lines changed: 82 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace ts.server {
3434

3535
export class SessionClient implements LanguageService {
3636
private sequence = 0;
37-
private lineMaps: ts.Map<number[]> = ts.createMap<number[]>();
37+
private lineMaps: Map<number[]> = createMap<number[]>();
3838
private messages: string[] = [];
3939
private lastRenameEntry: RenameEntry;
4040

@@ -53,33 +53,27 @@ namespace ts.server {
5353
let lineMap = this.lineMaps.get(fileName);
5454
if (!lineMap) {
5555
const scriptSnapshot = this.host.getScriptSnapshot(fileName);
56-
lineMap = ts.computeLineStarts(scriptSnapshot.getText(0, scriptSnapshot.getLength()));
56+
lineMap = computeLineStarts(scriptSnapshot.getText(0, scriptSnapshot.getLength()));
5757
this.lineMaps.set(fileName, lineMap);
5858
}
5959
return lineMap;
6060
}
6161

6262
private lineOffsetToPosition(fileName: string, lineOffset: protocol.Location, lineMap?: number[]): number {
6363
lineMap = lineMap || this.getLineMap(fileName);
64-
return ts.computePositionOfLineAndCharacter(lineMap, lineOffset.line - 1, lineOffset.offset - 1);
64+
return computePositionOfLineAndCharacter(lineMap, lineOffset.line - 1, lineOffset.offset - 1);
6565
}
6666

6767
private positionToOneBasedLineOffset(fileName: string, position: number): protocol.Location {
68-
const lineOffset = ts.computeLineAndCharacterOfPosition(this.getLineMap(fileName), position);
68+
const lineOffset = computeLineAndCharacterOfPosition(this.getLineMap(fileName), position);
6969
return {
7070
line: lineOffset.line + 1,
7171
offset: lineOffset.character + 1
7272
};
7373
}
7474

7575
private convertCodeEditsToTextChange(fileName: string, codeEdit: protocol.CodeEdit): ts.TextChange {
76-
const start = this.lineOffsetToPosition(fileName, codeEdit.start);
77-
const end = this.lineOffsetToPosition(fileName, codeEdit.end);
78-
79-
return {
80-
span: ts.createTextSpanFromBounds(start, end),
81-
newText: codeEdit.newText
82-
};
76+
return { span: this.decodeSpan(codeEdit, fileName), newText: codeEdit.newText };
8377
}
8478

8579
private processRequest<T extends protocol.Request>(command: string, args?: any): T {
@@ -154,13 +148,10 @@ namespace ts.server {
154148
const request = this.processRequest<protocol.QuickInfoRequest>(CommandNames.Quickinfo, args);
155149
const response = this.processResponse<protocol.QuickInfoResponse>(request);
156150

157-
const start = this.lineOffsetToPosition(fileName, response.body.start);
158-
const end = this.lineOffsetToPosition(fileName, response.body.end);
159-
160151
return {
161152
kind: response.body.kind,
162153
kindModifiers: response.body.kindModifiers,
163-
textSpan: ts.createTextSpanFromBounds(start, end),
154+
textSpan: this.decodeSpan(response.body, fileName),
164155
displayParts: [{ kind: "text", text: response.body.displayString }],
165156
documentation: [{ kind: "text", text: response.body.documentation }],
166157
tags: response.body.tags
@@ -192,11 +183,8 @@ namespace ts.server {
192183
entries: response.body.map(entry => {
193184

194185
if (entry.replacementSpan !== undefined) {
195-
const { name, kind, kindModifiers, sortText, replacementSpan} = entry;
196-
197-
const convertedSpan = createTextSpanFromBounds(this.lineOffsetToPosition(fileName, replacementSpan.start),
198-
this.lineOffsetToPosition(fileName, replacementSpan.end));
199-
return { name, kind, kindModifiers, sortText, replacementSpan: convertedSpan };
186+
const { name, kind, kindModifiers, sortText, replacementSpan } = entry;
187+
return { name, kind, kindModifiers, sortText, replacementSpan: this.decodeSpan(replacementSpan, fileName) };
200188
}
201189

202190
return entry as { name: string, kind: ScriptElementKind, kindModifiers: string, sortText: string };
@@ -226,36 +214,31 @@ namespace ts.server {
226214
const request = this.processRequest<protocol.NavtoRequest>(CommandNames.Navto, args);
227215
const response = this.processResponse<protocol.NavtoResponse>(request);
228216

229-
return response.body.map(entry => {
230-
const fileName = entry.file;
231-
const start = this.lineOffsetToPosition(fileName, entry.start);
232-
const end = this.lineOffsetToPosition(fileName, entry.end);
233-
234-
return {
235-
name: entry.name,
236-
containerName: entry.containerName || "",
237-
containerKind: entry.containerKind || ScriptElementKind.unknown,
238-
kind: entry.kind,
239-
kindModifiers: entry.kindModifiers,
240-
matchKind: entry.matchKind,
241-
isCaseSensitive: entry.isCaseSensitive,
242-
fileName,
243-
textSpan: ts.createTextSpanFromBounds(start, end)
244-
};
245-
});
217+
return response.body.map(entry => ({
218+
name: entry.name,
219+
containerName: entry.containerName || "",
220+
containerKind: entry.containerKind || ScriptElementKind.unknown,
221+
kind: entry.kind,
222+
kindModifiers: entry.kindModifiers,
223+
matchKind: entry.matchKind,
224+
isCaseSensitive: entry.isCaseSensitive,
225+
fileName: entry.file,
226+
textSpan: this.decodeSpan(entry),
227+
}));
246228
}
247229

248230
getFormattingEditsForRange(file: string, start: number, end: number, _options: ts.FormatCodeOptions): ts.TextChange[] {
249231
const args: protocol.FormatRequestArgs = this.createFileLocationRequestArgsWithEndLineAndOffset(file, start, end);
250232

233+
251234
// TODO: handle FormatCodeOptions
252235
const request = this.processRequest<protocol.FormatRequest>(CommandNames.Format, args);
253236
const response = this.processResponse<protocol.FormatResponse>(request);
254237

255238
return response.body.map(entry => this.convertCodeEditsToTextChange(file, entry));
256239
}
257240

258-
getFormattingEditsForDocument(fileName: string, options: ts.FormatCodeOptions): ts.TextChange[] {
241+
getFormattingEditsForDocument(fileName: string, options: FormatCodeOptions): ts.TextChange[] {
259242
return this.getFormattingEditsForRange(fileName, 0, this.host.getScriptSnapshot(fileName).getLength(), options);
260243
}
261244

@@ -275,19 +258,14 @@ namespace ts.server {
275258
const request = this.processRequest<protocol.DefinitionRequest>(CommandNames.Definition, args);
276259
const response = this.processResponse<protocol.DefinitionResponse>(request);
277260

278-
return response.body.map(entry => {
279-
const fileName = entry.file;
280-
const start = this.lineOffsetToPosition(fileName, entry.start);
281-
const end = this.lineOffsetToPosition(fileName, entry.end);
282-
return {
283-
containerKind: ScriptElementKind.unknown,
284-
containerName: "",
285-
fileName,
286-
textSpan: ts.createTextSpanFromBounds(start, end),
287-
kind: ScriptElementKind.unknown,
288-
name: ""
289-
};
290-
});
261+
return response.body.map(entry => ({
262+
containerKind: ScriptElementKind.unknown,
263+
containerName: "",
264+
fileName: entry.file,
265+
textSpan: this.decodeSpan(entry),
266+
kind: ScriptElementKind.unknown,
267+
name: ""
268+
}));
291269
}
292270

293271
getTypeDefinitionAtPosition(fileName: string, position: number): DefinitionInfo[] {
@@ -296,19 +274,14 @@ namespace ts.server {
296274
const request = this.processRequest<protocol.TypeDefinitionRequest>(CommandNames.TypeDefinition, args);
297275
const response = this.processResponse<protocol.TypeDefinitionResponse>(request);
298276

299-
return response.body.map(entry => {
300-
const fileName = entry.file;
301-
const start = this.lineOffsetToPosition(fileName, entry.start);
302-
const end = this.lineOffsetToPosition(fileName, entry.end);
303-
return {
304-
containerKind: ScriptElementKind.unknown,
305-
containerName: "",
306-
fileName,
307-
textSpan: ts.createTextSpanFromBounds(start, end),
308-
kind: ScriptElementKind.unknown,
309-
name: ""
310-
};
311-
});
277+
return response.body.map(entry => ({
278+
containerKind: ScriptElementKind.unknown,
279+
containerName: "",
280+
fileName: entry.file,
281+
textSpan: this.decodeSpan(entry),
282+
kind: ScriptElementKind.unknown,
283+
name: ""
284+
}));
312285
}
313286

314287
getImplementationAtPosition(fileName: string, position: number): ImplementationLocation[] {
@@ -317,17 +290,12 @@ namespace ts.server {
317290
const request = this.processRequest<protocol.ImplementationRequest>(CommandNames.Implementation, args);
318291
const response = this.processResponse<protocol.ImplementationResponse>(request);
319292

320-
return response.body.map(entry => {
321-
const fileName = entry.file;
322-
const start = this.lineOffsetToPosition(fileName, entry.start);
323-
const end = this.lineOffsetToPosition(fileName, entry.end);
324-
return {
325-
fileName,
326-
textSpan: ts.createTextSpanFromBounds(start, end),
327-
kind: ScriptElementKind.unknown,
328-
displayParts: []
329-
};
330-
});
293+
return response.body.map(entry => ({
294+
fileName: entry.file,
295+
textSpan: this.decodeSpan(entry),
296+
kind: ScriptElementKind.unknown,
297+
displayParts: []
298+
}));
331299
}
332300

333301
findReferences(_fileName: string, _position: number): ReferencedSymbol[] {
@@ -341,17 +309,12 @@ namespace ts.server {
341309
const request = this.processRequest<protocol.ReferencesRequest>(CommandNames.References, args);
342310
const response = this.processResponse<protocol.ReferencesResponse>(request);
343311

344-
return response.body.refs.map(entry => {
345-
const fileName = entry.file;
346-
const start = this.lineOffsetToPosition(fileName, entry.start);
347-
const end = this.lineOffsetToPosition(fileName, entry.end);
348-
return {
349-
fileName,
350-
textSpan: ts.createTextSpanFromBounds(start, end),
351-
isWriteAccess: entry.isWriteAccess,
352-
isDefinition: entry.isDefinition,
353-
};
354-
});
312+
return response.body.refs.map(entry => ({
313+
fileName: entry.file,
314+
textSpan: this.decodeSpan(entry),
315+
isWriteAccess: entry.isWriteAccess,
316+
isDefinition: entry.isDefinition,
317+
}));
355318
}
356319

357320
getEmitOutput(_fileName: string): EmitOutput {
@@ -406,22 +369,21 @@ namespace ts.server {
406369
const request = this.processRequest<protocol.RenameRequest>(CommandNames.Rename, args);
407370
const response = this.processResponse<protocol.RenameResponse>(request);
408371
const locations: RenameLocation[] = [];
409-
response.body.locs.map((entry: protocol.SpanGroup) => {
372+
for (const entry of response.body.locs) {
410373
const fileName = entry.file;
411-
entry.locs.map((loc: protocol.TextSpan) => {
412-
const start = this.lineOffsetToPosition(fileName, loc.start);
413-
const end = this.lineOffsetToPosition(fileName, loc.end);
414-
locations.push({ textSpan: ts.createTextSpanFromBounds(start, end), fileName, });
415-
});
416-
});
374+
for (const loc of entry.locs) {
375+
locations.push({ textSpan: this.decodeSpan(loc, fileName), fileName });
376+
}
377+
}
378+
417379
return this.lastRenameEntry = {
418380
canRename: response.body.info.canRename,
419381
displayName: response.body.info.displayName,
420382
fullDisplayName: response.body.info.fullDisplayName,
421383
kind: response.body.info.kind,
422384
kindModifiers: response.body.info.kindModifiers,
423385
localizedErrorMessage: response.body.info.localizedErrorMessage,
424-
triggerSpan: ts.createTextSpanFromBounds(position, position),
386+
triggerSpan: createTextSpanFromBounds(position, position),
425387
fileName,
426388
position,
427389
findInStrings,
@@ -485,7 +447,11 @@ namespace ts.server {
485447
return this.decodeNavigationTree(response.body, file, lineMap);
486448
}
487449

488-
private decodeSpan(span: protocol.TextSpan, fileName: string, lineMap: number[]) {
450+
private decodeSpan(span: protocol.TextSpan & { file: string }): TextSpan;
451+
private decodeSpan(span: protocol.TextSpan, fileName: string, lineMap?: number[]): TextSpan;
452+
private decodeSpan(span: protocol.TextSpan & { file: string }, fileName?: string, lineMap?: number[]): TextSpan {
453+
fileName = fileName || span.file;
454+
lineMap = lineMap || this.getLineMap(fileName);
489455
return createTextSpanFromBounds(
490456
this.lineOffsetToPosition(fileName, span.start, lineMap),
491457
this.lineOffsetToPosition(fileName, span.end, lineMap));
@@ -509,19 +475,11 @@ namespace ts.server {
509475
return undefined;
510476
}
511477

512-
const helpItems: protocol.SignatureHelpItems = response.body;
513-
const span = helpItems.applicableSpan;
514-
const start = this.lineOffsetToPosition(fileName, span.start);
515-
const end = this.lineOffsetToPosition(fileName, span.end);
516-
517-
const result: SignatureHelpItems = {
518-
items: helpItems.items,
519-
applicableSpan: { start, length: end - start },
520-
selectedItemIndex: helpItems.selectedItemIndex,
521-
argumentIndex: helpItems.argumentIndex,
522-
argumentCount: helpItems.argumentCount,
523-
};
524-
return result;
478+
const { items, applicableSpan: encodedApplicableSpan, selectedItemIndex, argumentIndex, argumentCount } = response.body;
479+
480+
const applicableSpan = this.decodeSpan(encodedApplicableSpan, fileName);
481+
482+
return { items, applicableSpan, selectedItemIndex, argumentIndex, argumentCount };
525483
}
526484

527485
getOccurrencesAtPosition(fileName: string, position: number): ReferenceEntry[] {
@@ -530,17 +488,12 @@ namespace ts.server {
530488
const request = this.processRequest<protocol.OccurrencesRequest>(CommandNames.Occurrences, args);
531489
const response = this.processResponse<protocol.OccurrencesResponse>(request);
532490

533-
return response.body.map(entry => {
534-
const fileName = entry.file;
535-
const start = this.lineOffsetToPosition(fileName, entry.start);
536-
const end = this.lineOffsetToPosition(fileName, entry.end);
537-
return {
538-
fileName,
539-
textSpan: ts.createTextSpanFromBounds(start, end),
540-
isWriteAccess: entry.isWriteAccess,
541-
isDefinition: false
542-
};
543-
});
491+
return response.body.map(entry => ({
492+
fileName: entry.file,
493+
textSpan: this.decodeSpan(entry),
494+
isWriteAccess: entry.isWriteAccess,
495+
isDefinition: false
496+
}));
544497
}
545498

546499
getDocumentHighlights(fileName: string, position: number, filesToSearch: string[]): DocumentHighlights[] {
@@ -549,26 +502,13 @@ namespace ts.server {
549502
const request = this.processRequest<protocol.DocumentHighlightsRequest>(CommandNames.DocumentHighlights, args);
550503
const response = this.processResponse<protocol.DocumentHighlightsResponse>(request);
551504

552-
const self = this;
553-
return response.body.map(convertToDocumentHighlights);
554-
555-
function convertToDocumentHighlights(item: ts.server.protocol.DocumentHighlightsItem): ts.DocumentHighlights {
556-
const { file, highlightSpans } = item;
557-
558-
return {
559-
fileName: file,
560-
highlightSpans: highlightSpans.map(convertHighlightSpan)
561-
};
562-
563-
function convertHighlightSpan(span: ts.server.protocol.HighlightSpan): ts.HighlightSpan {
564-
const start = self.lineOffsetToPosition(file, span.start);
565-
const end = self.lineOffsetToPosition(file, span.end);
566-
return {
567-
textSpan: ts.createTextSpanFromBounds(start, end),
568-
kind: span.kind
569-
};
570-
}
571-
}
505+
return response.body.map(item => ({
506+
fileName: item.file,
507+
highlightSpans: item.highlightSpans.map(span => ({
508+
textSpan: this.decodeSpan(span, item.file),
509+
kind: span.kind
510+
})),
511+
}));
572512
}
573513

574514
getOutliningSpans(_fileName: string): OutliningSpan[] {
@@ -662,7 +602,7 @@ namespace ts.server {
662602
};
663603
}
664604

665-
private convertCodeEditsToTextChanges(edits: ts.server.protocol.FileCodeEdits[]): FileTextChanges[] {
605+
private convertCodeEditsToTextChanges(edits: protocol.FileCodeEdits[]): FileTextChanges[] {
666606
return edits.map(edit => {
667607
const fileName = edit.fileName;
668608
return {
@@ -683,11 +623,8 @@ namespace ts.server {
683623
}
684624

685625
convertTextChangeToCodeEdit(change: protocol.CodeEdit, fileName: string): ts.TextChange {
686-
const start = this.lineOffsetToPosition(fileName, change.start);
687-
const end = this.lineOffsetToPosition(fileName, change.end);
688-
689626
return {
690-
span: { start, length: end - start },
627+
span: this.decodeSpan(change, fileName),
691628
newText: change.newText ? change.newText : ""
692629
};
693630
}
@@ -698,11 +635,7 @@ namespace ts.server {
698635
const request = this.processRequest<protocol.BraceRequest>(CommandNames.Brace, args);
699636
const response = this.processResponse<protocol.BraceResponse>(request);
700637

701-
return response.body.map(entry => {
702-
const start = this.lineOffsetToPosition(fileName, entry.start);
703-
const end = this.lineOffsetToPosition(fileName, entry.end);
704-
return { start, length: end - start };
705-
});
638+
return response.body.map(entry => this.decodeSpan(entry, fileName));
706639
}
707640

708641
getIndentationAtPosition(_fileName: string, _position: number, _options: EditorOptions): number {

0 commit comments

Comments
 (0)