mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
CodeMapper support (#55406)
This commit is contained in:
@@ -200,6 +200,7 @@ export const enum CommandTypes {
|
||||
ProvideCallHierarchyOutgoingCalls = "provideCallHierarchyOutgoingCalls",
|
||||
ProvideInlayHints = "provideInlayHints",
|
||||
WatchChange = "watchChange",
|
||||
MapCode = "mapCode",
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2342,6 +2343,38 @@ export interface InlayHintsResponse extends Response {
|
||||
body?: InlayHintItem[];
|
||||
}
|
||||
|
||||
export interface MapCodeRequestArgs extends FileRequestArgs {
|
||||
/**
|
||||
* The files and changes to try and apply/map.
|
||||
*/
|
||||
mapping: MapCodeRequestDocumentMapping;
|
||||
}
|
||||
|
||||
export interface MapCodeRequestDocumentMapping {
|
||||
/**
|
||||
* The specific code to map/insert/replace in the file.
|
||||
*/
|
||||
contents: string[];
|
||||
|
||||
/**
|
||||
* Areas of "focus" to inform the code mapper with. For example, cursor
|
||||
* location, current selection, viewport, etc. Nested arrays denote
|
||||
* priority: toplevel arrays are more important than inner arrays, and
|
||||
* inner array priorities are based on items within that array. Items
|
||||
* earlier in the arrays have higher priority.
|
||||
*/
|
||||
focusLocations?: TextSpan[][];
|
||||
}
|
||||
|
||||
export interface MapCodeRequest extends FileRequest {
|
||||
command: CommandTypes.MapCode;
|
||||
arguments: MapCodeRequestArgs;
|
||||
}
|
||||
|
||||
export interface MapCodeResponse extends Response {
|
||||
body: readonly FileCodeEdits[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Synchronous request for semantic diagnostics of one file.
|
||||
*/
|
||||
|
||||
@@ -1906,6 +1906,26 @@ export class Session<TMessage = string> implements EventSender {
|
||||
});
|
||||
}
|
||||
|
||||
private mapCode(args: protocol.MapCodeRequestArgs): protocol.FileCodeEdits[] {
|
||||
const formatOptions = this.getHostFormatOptions();
|
||||
const preferences = this.getHostPreferences();
|
||||
const { file, languageService } = this.getFileAndLanguageServiceForSyntacticOperation(args);
|
||||
const scriptInfo = this.projectService.getScriptInfoForNormalizedPath(file)!;
|
||||
const focusLocations = args.mapping.focusLocations?.map(spans => {
|
||||
return spans.map(loc => {
|
||||
const start = scriptInfo.lineOffsetToPosition(loc.start.line, loc.start.offset);
|
||||
const end = scriptInfo.lineOffsetToPosition(loc.end.line, loc.end.offset);
|
||||
return {
|
||||
start,
|
||||
length: end - start,
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
const changes = languageService.mapCode(file, args.mapping.contents, focusLocations, formatOptions, preferences);
|
||||
return this.mapTextChangesToCodeEdits(changes);
|
||||
}
|
||||
|
||||
private setCompilerOptionsForInferredProjects(args: protocol.SetCompilerOptionsForInferredProjectsArgs): void {
|
||||
this.projectService.setCompilerOptionsForInferredProjects(args.options, args.projectRootPath);
|
||||
}
|
||||
@@ -3610,6 +3630,9 @@ export class Session<TMessage = string> implements EventSender {
|
||||
[protocol.CommandTypes.ProvideInlayHints]: (request: protocol.InlayHintsRequest) => {
|
||||
return this.requiredResponse(this.provideInlayHints(request.arguments));
|
||||
},
|
||||
[protocol.CommandTypes.MapCode]: (request: protocol.MapCodeRequest) => {
|
||||
return this.requiredResponse(this.mapCode(request.arguments));
|
||||
},
|
||||
}));
|
||||
|
||||
public addProtocolHandler(command: string, handler: (request: protocol.Request) => HandlerResponse) {
|
||||
|
||||
Reference in New Issue
Block a user