Skip to content

Commit b9b79af

Browse files
committed
Recombining import completions and regular completion APIs
1 parent fb6ff42 commit b9b79af

File tree

7 files changed

+77
-112
lines changed

7 files changed

+77
-112
lines changed

src/harness/fourslash.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -780,10 +780,10 @@ namespace FourSlash {
780780
if (ranges && ranges.length > rangeIndex) {
781781
const range = ranges[rangeIndex];
782782

783-
const start = completions.textSpan.start;
784-
const end = start + completions.textSpan.length;
783+
const start = completion.replacementSpan.start;
784+
const end = start + completion.replacementSpan.length;
785785
if (range.start !== start || range.end !== end) {
786-
this.raiseError(`Expected completion span for '${symbol}', ${stringify(completions.textSpan)}, to cover range ${stringify(range)}`);
786+
this.raiseError(`Expected completion span for '${symbol}', ${stringify(completion.replacementSpan)}, to cover range ${stringify(range)}`);
787787
}
788788
}
789789
else {
@@ -892,7 +892,7 @@ namespace FourSlash {
892892
}
893893

894894
private getImportModuleCompletionListAtCaret() {
895-
return this.languageService.getImportModuleCompletionsAtPosition(this.activeFile.fileName, this.currentCaretPosition);
895+
return this.languageService.getCompletionsAtPosition(this.activeFile.fileName, this.currentCaretPosition);
896896
}
897897

898898
private getCompletionEntryDetails(entryName: string) {

src/harness/harnessLanguageService.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,9 +405,6 @@ namespace Harness.LanguageService {
405405
getCompletionsAtPosition(fileName: string, position: number): ts.CompletionInfo {
406406
return unwrapJSONCallResult(this.shim.getCompletionsAtPosition(fileName, position));
407407
}
408-
getImportModuleCompletionsAtPosition(fileName: string, position: number): ts.ImportCompletionInfo {
409-
return unwrapJSONCallResult(this.shim.getImportModuleCompletionsAtPosition(fileName, position));
410-
}
411408
getCompletionEntryDetails(fileName: string, position: number, entryName: string): ts.CompletionEntryDetails {
412409
return unwrapJSONCallResult(this.shim.getCompletionEntryDetails(fileName, position, entryName));
413410
}

src/server/client.ts

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -216,28 +216,16 @@ namespace ts.server {
216216
return {
217217
isMemberCompletion: false,
218218
isNewIdentifierLocation: false,
219-
entries: response.body
220-
};
221-
}
219+
entries: response.body.map(({ name, kind, kindModifiers, sortText, replacementSpan }) => {
222220

223-
getImportModuleCompletionsAtPosition(fileName: string, position: number): ImportCompletionInfo {
224-
const lineOffset = this.positionToOneBasedLineOffset(fileName, position);
225-
const args: protocol.CompletionsRequestArgs = {
226-
file: fileName,
227-
line: lineOffset.line,
228-
offset: lineOffset.offset,
229-
prefix: undefined
230-
};
231-
232-
const request = this.processRequest<protocol.ImportModuleCompletionsRequest>(CommandNames.ImportModuleCompletions, args);
233-
const response = this.processResponse<protocol.ImportModuleCompletionsResponse>(request);
234-
235-
const startPosition = this.lineOffsetToPosition(fileName, response.span.start);
236-
const endPosition = this.lineOffsetToPosition(fileName, response.span.end);
221+
let convertedSpan: TextSpan;
222+
if (replacementSpan) {
223+
convertedSpan = createTextSpanFromBounds(this.lineOffsetToPosition(fileName, replacementSpan.start),
224+
this.lineOffsetToPosition(fileName, replacementSpan.end));
225+
}
237226

238-
return {
239-
textSpan: ts.createTextSpanFromBounds(startPosition, endPosition),
240-
entries: response.body
227+
return { name, kind, kindModifiers, sortText, replacementSpan: convertedSpan };
228+
})
241229
};
242230
}
243231

src/server/protocol.d.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -717,16 +717,6 @@ declare namespace ts.server.protocol {
717717
arguments: CompletionsRequestArgs;
718718
}
719719

720-
/**
721-
* Import Module Completions request; value of command field is
722-
* "importModuleCompletions". Given a file location (file, line,
723-
* col) return the possible completions for external module
724-
* specifiers or paths given that position refers to a module
725-
* import declaration, require call, or triple slash reference.
726-
*/
727-
export interface ImportModuleCompletionsRequest extends FileLocationRequest {
728-
}
729-
730720
/**
731721
* Arguments for completion details request.
732722
*/
@@ -783,6 +773,11 @@ declare namespace ts.server.protocol {
783773
* is often the same as the name but may be different in certain circumstances.
784774
*/
785775
sortText: string;
776+
/**
777+
* An optional span that indicates the text to be replaced by this completion item. If present,
778+
* this span should be used instead of the default one.
779+
*/
780+
replacementSpan?: TextSpan;
786781
}
787782

788783
/**
@@ -816,11 +811,6 @@ declare namespace ts.server.protocol {
816811
body?: CompletionEntry[];
817812
}
818813

819-
export interface ImportModuleCompletionsResponse extends Response {
820-
span: TextSpan;
821-
body?: ImportCompletionEntry[];
822-
}
823-
824814
export interface CompletionDetailsResponse extends Response {
825815
body?: CompletionEntryDetails[];
826816
}

src/server/session.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ namespace ts.server {
103103
export const Change = "change";
104104
export const Close = "close";
105105
export const Completions = "completions";
106-
export const ImportModuleCompletions = "importModuleCompletions";
107106
export const CompletionDetails = "completionEntryDetails";
108107
export const Configure = "configure";
109108
export const Definition = "definition";
@@ -773,7 +772,17 @@ namespace ts.server {
773772

774773
return completions.entries.reduce((result: protocol.CompletionEntry[], entry: ts.CompletionEntry) => {
775774
if (completions.isMemberCompletion || (entry.name.toLowerCase().indexOf(prefix.toLowerCase()) === 0)) {
776-
result.push(entry);
775+
const { name, kind, kindModifiers, sortText, replacementSpan } = entry;
776+
777+
let convertedSpan: protocol.TextSpan = undefined;
778+
if (replacementSpan) {
779+
convertedSpan = {
780+
start: compilerService.host.positionToLineOffset(fileName, replacementSpan.start),
781+
end: compilerService.host.positionToLineOffset(fileName, replacementSpan.start + replacementSpan.length)
782+
};
783+
}
784+
785+
result.push({ name, kind, kindModifiers, sortText, replacementSpan: convertedSpan });
777786
}
778787
return result;
779788
}, []).sort((a, b) => a.name.localeCompare(b.name));

0 commit comments

Comments
 (0)