Compare commits
2 commits
main
...
gabritto/s
Author | SHA1 | Date | |
---|---|---|---|
64e0bd4263 | |||
3e59096506 |
|
@ -1940,7 +1940,7 @@ namespace ts {
|
|||
emitPlaceholder(hint, node, snippet);
|
||||
break;
|
||||
case SnippetKind.TabStop:
|
||||
emitTabStop(snippet);
|
||||
emitTabStop(hint, node, snippet);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -1952,7 +1952,12 @@ namespace ts {
|
|||
// `${2:...}`
|
||||
}
|
||||
|
||||
function emitTabStop(snippet: TabStop) {
|
||||
function emitTabStop(hint: EmitHint, node: Node, snippet: TabStop) {
|
||||
// A tab stop should only be attached to an empty node, i.e. a node that doesn't emit any text.
|
||||
Debug.assert(node.kind === SyntaxKind.EmptyStatement,
|
||||
`A tab stop cannot be attached to a node of kind ${Debug.formatSyntaxKind(node.kind)}.`);
|
||||
Debug.assert(hint !== EmitHint.EmbeddedStatement,
|
||||
`A tab stop cannot be attached to an embedded statement.`);
|
||||
nonEscapingWrite(`\$${snippet.order}`);
|
||||
}
|
||||
|
||||
|
@ -4109,10 +4114,14 @@ namespace ts {
|
|||
}
|
||||
|
||||
function emitModifiers(node: Node, modifiers: NodeArray<Modifier> | undefined) {
|
||||
if (modifiers && modifiers.length) {
|
||||
if (modifiers) {
|
||||
onBeforeEmitNodeArray?.(modifiers);
|
||||
if (modifiers.length) {
|
||||
emitList(node, modifiers, ListFormat.Modifiers);
|
||||
writeSpace();
|
||||
}
|
||||
onAfterEmitNodeArray?.(modifiers);
|
||||
}
|
||||
}
|
||||
|
||||
function emitTypeAnnotation(node: TypeNode | undefined) {
|
||||
|
|
|
@ -472,8 +472,8 @@ namespace Harness.LanguageService {
|
|||
const responseFormat = format || ts.SemanticClassificationFormat.Original;
|
||||
return unwrapJSONCallResult(this.shim.getEncodedSemanticClassifications(fileName, span.start, span.length, responseFormat));
|
||||
}
|
||||
getCompletionsAtPosition(fileName: string, position: number, preferences: ts.UserPreferences | undefined): ts.CompletionInfo {
|
||||
return unwrapJSONCallResult(this.shim.getCompletionsAtPosition(fileName, position, preferences));
|
||||
getCompletionsAtPosition(fileName: string, position: number, preferences: ts.UserPreferences | undefined, formattingSettings: ts.FormatCodeSettings | undefined): ts.CompletionInfo {
|
||||
return unwrapJSONCallResult(this.shim.getCompletionsAtPosition(fileName, position, preferences, formattingSettings));
|
||||
}
|
||||
getCompletionEntryDetails(fileName: string, position: number, entryName: string, formatOptions: ts.FormatCodeOptions | undefined, source: string | undefined, preferences: ts.UserPreferences | undefined, data: ts.CompletionEntryData | undefined): ts.CompletionEntryDetails {
|
||||
return unwrapJSONCallResult(this.shim.getCompletionEntryDetails(fileName, position, entryName, JSON.stringify(formatOptions), source, preferences, data));
|
||||
|
|
|
@ -1842,13 +1842,18 @@ namespace ts.server {
|
|||
const scriptInfo = this.projectService.getScriptInfoForNormalizedPath(file)!;
|
||||
const position = this.getPosition(args, scriptInfo);
|
||||
|
||||
const completions = project.getLanguageService().getCompletionsAtPosition(file, position, {
|
||||
const completions = project.getLanguageService().getCompletionsAtPosition(
|
||||
file,
|
||||
position,
|
||||
{
|
||||
...convertUserPreferences(this.getPreferences(file)),
|
||||
triggerCharacter: args.triggerCharacter,
|
||||
triggerKind: args.triggerKind as CompletionTriggerKind | undefined,
|
||||
includeExternalModuleExports: args.includeExternalModuleExports,
|
||||
includeInsertTextCompletions: args.includeInsertTextCompletions
|
||||
});
|
||||
includeInsertTextCompletions: args.includeInsertTextCompletions,
|
||||
},
|
||||
project.projectService.getFormatCodeOptions(file),
|
||||
);
|
||||
if (completions === undefined) return undefined;
|
||||
|
||||
if (kind === protocol.CommandTypes.CompletionsFull) return completions;
|
||||
|
|
|
@ -229,6 +229,7 @@ namespace ts.Completions {
|
|||
triggerCharacter: CompletionsTriggerCharacter | undefined,
|
||||
completionKind: CompletionTriggerKind | undefined,
|
||||
cancellationToken: CancellationToken,
|
||||
formatContext?: formatting.FormatContext,
|
||||
): CompletionInfo | undefined {
|
||||
const { previousToken } = getRelevantTokens(position, sourceFile);
|
||||
if (triggerCharacter && !isInString(sourceFile, position, previousToken) && !isValidTrigger(sourceFile, triggerCharacter, previousToken, position)) {
|
||||
|
@ -275,7 +276,7 @@ namespace ts.Completions {
|
|||
|
||||
switch (completionData.kind) {
|
||||
case CompletionDataKind.Data:
|
||||
const response = completionInfoFromData(sourceFile, host, program, compilerOptions, log, completionData, preferences);
|
||||
const response = completionInfoFromData(sourceFile, host, program, compilerOptions, log, completionData, preferences, formatContext);
|
||||
if (response?.isIncomplete) {
|
||||
incompleteCompletionsCache?.set(response);
|
||||
}
|
||||
|
@ -412,6 +413,7 @@ namespace ts.Completions {
|
|||
log: Log,
|
||||
completionData: CompletionData,
|
||||
preferences: UserPreferences,
|
||||
formatContext: formatting.FormatContext | undefined,
|
||||
): CompletionInfo | undefined {
|
||||
const {
|
||||
symbols,
|
||||
|
@ -459,6 +461,7 @@ namespace ts.Completions {
|
|||
completionKind,
|
||||
preferences,
|
||||
compilerOptions,
|
||||
formatContext,
|
||||
isTypeOnlyLocation,
|
||||
propertyAccessToConvert,
|
||||
isJsxIdentifierExpected,
|
||||
|
@ -489,6 +492,7 @@ namespace ts.Completions {
|
|||
completionKind,
|
||||
preferences,
|
||||
compilerOptions,
|
||||
formatContext,
|
||||
isTypeOnlyLocation,
|
||||
propertyAccessToConvert,
|
||||
isJsxIdentifierExpected,
|
||||
|
@ -638,6 +642,7 @@ namespace ts.Completions {
|
|||
options: CompilerOptions,
|
||||
preferences: UserPreferences,
|
||||
completionKind: CompletionKind,
|
||||
formatContext: formatting.FormatContext | undefined,
|
||||
): CompletionEntry | undefined {
|
||||
let insertText: string | undefined;
|
||||
let replacementSpan = getReplacementSpanForContextToken(replacementToken);
|
||||
|
@ -706,7 +711,7 @@ namespace ts.Completions {
|
|||
completionKind === CompletionKind.MemberLike &&
|
||||
isClassLikeMemberCompletion(symbol, location)) {
|
||||
let importAdder;
|
||||
({ insertText, isSnippet, importAdder } = getEntryForMemberCompletion(host, program, options, preferences, name, symbol, location, contextToken));
|
||||
({ insertText, isSnippet, importAdder } = getEntryForMemberCompletion(host, program, options, preferences, name, symbol, location, contextToken, formatContext));
|
||||
if (importAdder?.hasFixes()) {
|
||||
hasAction = true;
|
||||
source = CompletionSource.ClassMemberSnippet;
|
||||
|
@ -832,6 +837,7 @@ namespace ts.Completions {
|
|||
symbol: Symbol,
|
||||
location: Node,
|
||||
contextToken: Node | undefined,
|
||||
formatContext: formatting.FormatContext | undefined,
|
||||
): { insertText: string, isSnippet?: true, importAdder?: codefix.ImportAdder } {
|
||||
const classLikeDeclaration = findAncestor(location, isClassLike);
|
||||
if (!classLikeDeclaration) {
|
||||
|
@ -852,15 +858,16 @@ namespace ts.Completions {
|
|||
});
|
||||
const importAdder = codefix.createImportAdder(sourceFile, program, preferences, host);
|
||||
|
||||
// Create empty body for possible method implementation.
|
||||
let body;
|
||||
if (preferences.includeCompletionsWithSnippetText) {
|
||||
isSnippet = true;
|
||||
// We are adding a tabstop (i.e. `$0`) in the body of the suggested member,
|
||||
// if it has one, so that the cursor ends up in the body once the completion is inserted.
|
||||
// Note: this assumes we won't have more than one body in the completion nodes, which should be the case.
|
||||
const emptyStatement = factory.createExpressionStatement(factory.createIdentifier(""));
|
||||
setSnippetElement(emptyStatement, { kind: SnippetKind.TabStop, order: 0 });
|
||||
body = factory.createBlock([emptyStatement], /* multiline */ true);
|
||||
const emptyStmt = factory.createEmptyStatement();
|
||||
body = factory.createBlock([emptyStmt], /* multiline */ true);
|
||||
setSnippetElement(emptyStmt, { kind: SnippetKind.TabStop, order: 0 });
|
||||
}
|
||||
else {
|
||||
body = factory.createBlock([], /* multiline */ true);
|
||||
|
@ -911,7 +918,6 @@ namespace ts.Completions {
|
|||
modifiers = node.modifierFlagsCache | requiredModifiers | presentModifiers;
|
||||
}
|
||||
node = factory.updateModifiers(node, modifiers & (~presentModifiers));
|
||||
|
||||
completionNodes.push(node);
|
||||
},
|
||||
body,
|
||||
|
@ -919,11 +925,39 @@ namespace ts.Completions {
|
|||
isAbstract);
|
||||
|
||||
if (completionNodes.length) {
|
||||
// If we have access to formatting settings, we print the nodes using the emitter,
|
||||
// and then format the printed text.
|
||||
if (formatContext) {
|
||||
const syntheticFile = {
|
||||
text: printer.printSnippetList(
|
||||
ListFormat.MultiLine | ListFormat.NoTrailingNewLine,
|
||||
factory.createNodeArray(completionNodes),
|
||||
sourceFile),
|
||||
getLineAndCharacterOfPosition(pos: number) {
|
||||
return getLineAndCharacterOfPosition(this, pos);
|
||||
},
|
||||
};
|
||||
|
||||
const formatOptions = getFormatCodeSettingsForWriting(formatContext, sourceFile);
|
||||
const changes = flatMap(completionNodes, node => {
|
||||
const nodeWithPos = textChanges.assignPositionsToNode(node);
|
||||
return formatting.formatNodeGivenIndentation(
|
||||
nodeWithPos,
|
||||
syntheticFile,
|
||||
sourceFile.languageVariant,
|
||||
/* indentation */ 0,
|
||||
/* delta */ 0,
|
||||
{ ...formatContext, options: formatOptions });
|
||||
});
|
||||
insertText = textChanges.applyChanges(syntheticFile.text, changes);
|
||||
}
|
||||
else { // Otherwise, just use emitter to print the new nodes.
|
||||
insertText = printer.printSnippetList(
|
||||
ListFormat.MultiLine | ListFormat.NoTrailingNewLine,
|
||||
factory.createNodeArray(completionNodes),
|
||||
sourceFile);
|
||||
}
|
||||
}
|
||||
|
||||
return { insertText, isSnippet, importAdder };
|
||||
}
|
||||
|
@ -972,8 +1006,8 @@ namespace ts.Completions {
|
|||
function createSnippetPrinter(
|
||||
printerOptions: PrinterOptions,
|
||||
) {
|
||||
const printer = createPrinter(printerOptions);
|
||||
const baseWriter = createTextWriter(getNewLineCharacter(printerOptions));
|
||||
const baseWriter = textChanges.createWriter(getNewLineCharacter(printerOptions));
|
||||
const printer = createPrinter(printerOptions, baseWriter);
|
||||
const writer: EmitTextWriter = {
|
||||
...baseWriter,
|
||||
write: s => baseWriter.write(escapeSnippetText(s)),
|
||||
|
@ -1117,6 +1151,7 @@ namespace ts.Completions {
|
|||
kind: CompletionKind,
|
||||
preferences: UserPreferences,
|
||||
compilerOptions: CompilerOptions,
|
||||
formatContext: formatting.FormatContext | undefined,
|
||||
isTypeOnlyLocation?: boolean,
|
||||
propertyAccessToConvert?: PropertyAccessExpression,
|
||||
jsxIdentifierExpected?: boolean,
|
||||
|
@ -1166,6 +1201,7 @@ namespace ts.Completions {
|
|||
compilerOptions,
|
||||
preferences,
|
||||
kind,
|
||||
formatContext,
|
||||
);
|
||||
if (!entry) {
|
||||
continue;
|
||||
|
@ -1444,7 +1480,8 @@ namespace ts.Completions {
|
|||
name,
|
||||
symbol,
|
||||
location,
|
||||
contextToken);
|
||||
contextToken,
|
||||
formatContext);
|
||||
if (importAdder) {
|
||||
const changes = textChanges.ChangeTracker.with(
|
||||
{ host, formatContext, preferences },
|
||||
|
|
|
@ -1593,7 +1593,7 @@ namespace ts {
|
|||
return [...program.getOptionsDiagnostics(cancellationToken), ...program.getGlobalDiagnostics(cancellationToken)];
|
||||
}
|
||||
|
||||
function getCompletionsAtPosition(fileName: string, position: number, options: GetCompletionsAtPositionOptions = emptyOptions): CompletionInfo | undefined {
|
||||
function getCompletionsAtPosition(fileName: string, position: number, options: GetCompletionsAtPositionOptions = emptyOptions, formattingSettings?: FormatCodeSettings): CompletionInfo | undefined {
|
||||
// Convert from deprecated options names to new names
|
||||
const fullPreferences: UserPreferences = {
|
||||
...identity<UserPreferences>(options), // avoid excess property check
|
||||
|
@ -1610,7 +1610,8 @@ namespace ts {
|
|||
fullPreferences,
|
||||
options.triggerCharacter,
|
||||
options.triggerKind,
|
||||
cancellationToken);
|
||||
cancellationToken,
|
||||
formattingSettings && formatting.getFormatContext(formattingSettings, host));
|
||||
}
|
||||
|
||||
function getCompletionEntryDetails(fileName: string, position: number, name: string, formattingOptions: FormatCodeSettings | undefined, source: string | undefined, preferences: UserPreferences = emptyOptions, data?: CompletionEntryData): CompletionEntryDetails | undefined {
|
||||
|
|
|
@ -151,7 +151,7 @@ namespace ts {
|
|||
getEncodedSyntacticClassifications(fileName: string, start: number, length: number): string;
|
||||
getEncodedSemanticClassifications(fileName: string, start: number, length: number, format?: SemanticClassificationFormat): string;
|
||||
|
||||
getCompletionsAtPosition(fileName: string, position: number, preferences: UserPreferences | undefined): string;
|
||||
getCompletionsAtPosition(fileName: string, position: number, preferences: UserPreferences | undefined, formattingSettings: FormatCodeSettings | undefined): string;
|
||||
getCompletionEntryDetails(fileName: string, position: number, entryName: string, formatOptions: string/*Services.FormatCodeOptions*/ | undefined, source: string | undefined, preferences: UserPreferences | undefined, data: CompletionEntryData | undefined): string;
|
||||
|
||||
getQuickInfoAtPosition(fileName: string, position: number): string;
|
||||
|
@ -956,10 +956,10 @@ namespace ts {
|
|||
* to provide at the given source position and providing a member completion
|
||||
* list if requested.
|
||||
*/
|
||||
public getCompletionsAtPosition(fileName: string, position: number, preferences: GetCompletionsAtPositionOptions | undefined) {
|
||||
public getCompletionsAtPosition(fileName: string, position: number, preferences: GetCompletionsAtPositionOptions | undefined, formattingSettings: FormatCodeSettings | undefined) {
|
||||
return this.forwardJSONCall(
|
||||
`getCompletionsAtPosition('${fileName}', ${position}, ${preferences})`,
|
||||
() => this.languageService.getCompletionsAtPosition(fileName, position, preferences)
|
||||
`getCompletionsAtPosition('${fileName}', ${position}, ${preferences}, ${formattingSettings})`,
|
||||
() => this.languageService.getCompletionsAtPosition(fileName, position, preferences, formattingSettings)
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -54,6 +54,7 @@ namespace ts.Completions.StringCompletions {
|
|||
CompletionKind.String,
|
||||
preferences,
|
||||
options,
|
||||
/*formatContext*/ undefined,
|
||||
); // Target will not be used, so arbitrary
|
||||
return { isGlobalCompletion: false, isMemberCompletion: true, isNewIdentifierLocation: completion.hasIndexSignature, optionalReplacementSpan, entries };
|
||||
}
|
||||
|
|
|
@ -1052,15 +1052,6 @@ namespace ts.textChanges {
|
|||
? "" : options.suffix);
|
||||
}
|
||||
|
||||
function getFormatCodeSettingsForWriting({ options }: formatting.FormatContext, sourceFile: SourceFile): FormatCodeSettings {
|
||||
const shouldAutoDetectSemicolonPreference = !options.semicolons || options.semicolons === SemicolonPreference.Ignore;
|
||||
const shouldRemoveSemicolons = options.semicolons === SemicolonPreference.Remove || shouldAutoDetectSemicolonPreference && !probablyUsesSemicolons(sourceFile);
|
||||
return {
|
||||
...options,
|
||||
semicolons: shouldRemoveSemicolons ? SemicolonPreference.Remove : SemicolonPreference.Ignore,
|
||||
};
|
||||
}
|
||||
|
||||
/** Note: this may mutate `nodeIn`. */
|
||||
function getFormattedTextOfNode(nodeIn: Node, sourceFile: SourceFile, pos: number, { indentation, prefix, delta }: InsertNodeOptions, newLineCharacter: string, formatContext: formatting.FormatContext, validate: ValidateNonFormattedText | undefined): string {
|
||||
const { node, text } = getNonformattedText(nodeIn, sourceFile, newLineCharacter);
|
||||
|
@ -1110,7 +1101,7 @@ namespace ts.textChanges {
|
|||
return skipTrivia(s, 0) === s.length;
|
||||
}
|
||||
|
||||
function assignPositionsToNode(node: Node): Node {
|
||||
export function assignPositionsToNode(node: Node): Node {
|
||||
const visited = visitEachChild(node, assignPositionsToNode, nullTransformationContext, assignPositionsToNodeArray, assignPositionsToNode);
|
||||
// create proxy node for non synthesized nodes
|
||||
const newNode = nodeIsSynthesized(visited) ? visited : Object.create(visited) as Node;
|
||||
|
@ -1131,7 +1122,7 @@ namespace ts.textChanges {
|
|||
|
||||
interface TextChangesWriter extends EmitTextWriter, PrintHandlers {}
|
||||
|
||||
function createWriter(newLine: string): TextChangesWriter {
|
||||
export function createWriter(newLine: string): TextChangesWriter {
|
||||
let lastNonTriviaPosition = 0;
|
||||
|
||||
const writer = createTextWriter(newLine);
|
||||
|
|
|
@ -414,8 +414,9 @@ namespace ts {
|
|||
* @param position A zero-based index of the character where you want the entries
|
||||
* @param options An object describing how the request was triggered and what kinds
|
||||
* of code actions can be returned with the completions.
|
||||
* @param formattingSettings settings needed for calling formatting functions.
|
||||
*/
|
||||
getCompletionsAtPosition(fileName: string, position: number, options: GetCompletionsAtPositionOptions | undefined): WithMetadata<CompletionInfo> | undefined;
|
||||
getCompletionsAtPosition(fileName: string, position: number, options: GetCompletionsAtPositionOptions | undefined, formattingSettings?: FormatCodeSettings): WithMetadata<CompletionInfo> | undefined;
|
||||
|
||||
/**
|
||||
* Gets the extended details for a completion entry retrieved from `getCompletionsAtPosition`.
|
||||
|
|
|
@ -3290,5 +3290,17 @@ namespace ts {
|
|||
: getLocaleSpecificMessage(diag);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get format code settings for a code writing context (e.g. when formatting text changes or completions code).
|
||||
*/
|
||||
export function getFormatCodeSettingsForWriting({ options }: formatting.FormatContext, sourceFile: SourceFile): FormatCodeSettings {
|
||||
const shouldAutoDetectSemicolonPreference = !options.semicolons || options.semicolons === SemicolonPreference.Ignore;
|
||||
const shouldRemoveSemicolons = options.semicolons === SemicolonPreference.Remove || shouldAutoDetectSemicolonPreference && !probablyUsesSemicolons(sourceFile);
|
||||
return {
|
||||
...options,
|
||||
semicolons: shouldRemoveSemicolons ? SemicolonPreference.Remove : SemicolonPreference.Ignore,
|
||||
};
|
||||
}
|
||||
|
||||
// #endregion
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue