Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
41913b7
skeleton of new feature
gabritto Feb 23, 2022
d5038ce
working prototype
gabritto Feb 25, 2022
fca74c1
refactor print and format code into its own function
gabritto Feb 25, 2022
249c3ba
minor changes; don't support overloads
gabritto Mar 1, 2022
ef15f81
have two completion entries
gabritto Mar 2, 2022
1f26a52
get rid of accessor support
gabritto Mar 4, 2022
1c28090
add snippet support
gabritto Mar 4, 2022
e628590
add formatting
gabritto Mar 4, 2022
515fc73
add trailing comma
gabritto Mar 4, 2022
0229a7d
add sourcedisplay
gabritto Mar 5, 2022
9892dc8
support auto-imports via completion details
gabritto Mar 5, 2022
8550bba
add user preference option and fix ordering of entries
gabritto Mar 7, 2022
78c7d9e
cleanup
gabritto Mar 7, 2022
1f67d97
don't return code actions for no import fixes
gabritto Mar 8, 2022
6c96290
Merge branch 'main' into gabritto/issue46590
gabritto Mar 8, 2022
814561f
make sortText lower priority for snippets
gabritto Mar 9, 2022
088904e
get rid of flag
gabritto Mar 9, 2022
4e63276
use optional member sort text
gabritto Mar 9, 2022
474d9a9
update baselines
gabritto Mar 9, 2022
b5d05c0
don't collect method symbols if insert text is not supported
gabritto Mar 9, 2022
6914576
remove comment
gabritto Mar 16, 2022
0e0ae05
return undefined if type is not function type
gabritto Mar 17, 2022
36eac73
only slice if needed
gabritto Mar 17, 2022
b9cb703
use union reduction; more test cases
gabritto Mar 17, 2022
7c9cbdb
WIP: modify sort text system
gabritto Mar 21, 2022
07c4fcf
Improve new sort text system
gabritto Mar 22, 2022
5b0d86d
add signature and union type check
gabritto Mar 22, 2022
b032aa5
Merge branch 'main' into gabritto/issue46590
gabritto Mar 23, 2022
08a55c3
re-add flag
gabritto Mar 23, 2022
6ebe361
fix tests
gabritto Mar 23, 2022
411bc18
rename sort text helper
gabritto Mar 23, 2022
1ebdf3d
fix test and code for union case
gabritto Mar 24, 2022
5477408
add new flag to protocol type
gabritto Mar 24, 2022
af7de76
fix spaces
gabritto Mar 24, 2022
1b884c4
CR: minor fixes
gabritto Mar 29, 2022
3820f32
CR: more fixes
gabritto Mar 29, 2022
e7a51e6
CR: restructure main flow
gabritto Mar 29, 2022
203aab0
minor fix
gabritto Mar 29, 2022
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
get rid of accessor support
  • Loading branch information
gabritto committed Mar 4, 2022
commit 1f26a523b97ef7d0399a27a7f3f77febf5e1610a
63 changes: 7 additions & 56 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,6 @@ namespace ts.Completions {
if (!originIsObjectLiteralMember(origin)) {
return undefined;
}
// >> TODO: check completion kind is memberlike
// TODO: support JS files.
if (isInJSFile(location)) {
return undefined;
Expand All @@ -1087,14 +1086,11 @@ namespace ts.Completions {
`type Foo = {
bar(x: number): void;
foo: (x: string) => string;
get prop(): number;
set prop(n: number);
}`,
`bar` will have symbol flag `Method`,
`foo` will have symbol flag `Property`.
`prop` will have symbol flags `GetAccessor` and `SetAccessor`.
*/
if (!(symbol.flags & (SymbolFlags.Property | SymbolFlags.Method | SymbolFlags.Accessor))) {
if (!(symbol.flags & (SymbolFlags.Property | SymbolFlags.Method))) {
return undefined;
}

Expand Down Expand Up @@ -1127,8 +1123,8 @@ namespace ts.Completions {
const style = ObjectLiteralFunctionPropertyStyle.ArrowFunction; // >> TODO: Get this from somewhere
const importAdder = codefix.createImportAdder(sourceFile, program, preferences, host);
const body = factory.createBlock([]);
const functionProps = createObjectLiteralFunctionProperty(symbol, enclosingDeclaration, sourceFile, program, host, preferences, importAdder, /*body*/ body, style);
if (!functionProps) {
const functionProp = createObjectLiteralFunctionProperty(symbol, enclosingDeclaration, sourceFile, program, host, preferences, importAdder, /*body*/ body, style);
if (!functionProp) {
return undefined;
}

Expand All @@ -1141,15 +1137,15 @@ namespace ts.Completions {
});

// insertText = printer.printSnippet(EmitHint.Unspecified, functionProp, sourceFile);
const format = ListFormat.CommaDelimited;
// if (formatContext) {
// // >> TODO: this is breaking get/set pairs because they're on the same line when printing
// insertText = printer.printAndFormatSnippetList(format, factory.createNodeArray(functionProps), sourceFile, formatContext);
// }
// else {
// insertText = printer.printSnippetList(format, factory.createNodeArray(functionProps), sourceFile);
// }
insertText = printer.printSnippetList(format, factory.createNodeArray(functionProps), sourceFile);
// insertText = printer.printSnippet(format, factory.createNodeArray(functionProps), sourceFile);
insertText = printer.printSnippet(EmitHint.Unspecified, functionProp, sourceFile);

return { isSnippet, insertText, importAdder };
};
Expand All @@ -1164,7 +1160,7 @@ namespace ts.Completions {
importAdder: codefix.ImportAdder,
body: Block,
style: ObjectLiteralFunctionPropertyStyle,
): (PropertyAssignment | MethodDeclaration | AccessorDeclaration)[] | undefined {
): PropertyAssignment | MethodDeclaration | undefined {
const declarations = symbol.getDeclarations();
if (!(declarations && declarations.length)) {
return undefined;
Expand All @@ -1187,52 +1183,7 @@ namespace ts.Completions {
// We don't support overloads in object literals.
return undefined;
}
const prop = createFunctionFromType(type, style);
return prop && [prop];
}
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor: {
// >> TODO: confusing to return both accessors in a completion scenario? They're kinda different functions. Return in different entries
let typeNode = checker.typeToTypeNode(type, enclosingDeclaration, builderFlags, codefix.getNoopSymbolTrackerWithResolver({ program, host }));
const importableReference = codefix.tryGetAutoImportableReferenceFromTypeNode(typeNode, scriptTarget);
if (importableReference) {
typeNode = importableReference.typeNode;
codefix.importSymbols(importAdder, importableReference.symbols);
}
const accessors = getAllAccessorDeclarations(declarations, declaration as AccessorDeclaration);
// >> Why does the order matter? Or maybe it doesn't, and we're just trying to get all the accessors?
const orderedAccessors = accessors.secondAccessor ? [accessors.firstAccessor, accessors.secondAccessor] : [accessors.firstAccessor];
const nodes = [];
for (const accessor of orderedAccessors) {
if (isGetAccessorDeclaration(accessor)) {
nodes.push(factory.createGetAccessorDeclaration(
/*decorators*/ undefined,
/*modifiers*/ undefined,
name,
emptyArray,
typeNode,
body || factory.createBlock([])));
}
else {
Debug.assertNode(accessor, isSetAccessorDeclaration, "The counterpart to a getter should be a setter");
const accessorParameter = getSetAccessorValueParameter(accessor);
const paramName = accessorParameter && isIdentifier(accessorParameter.name) ? idText(accessorParameter.name) : "arg";
const newParameter = factory.createParameterDeclaration(
/*decorators*/ undefined,
/*modifiers*/ undefined,
/*dotDotDotToken*/ undefined,
paramName,
/*questionToken*/ undefined,
typeNode);
nodes.push(factory.createSetAccessorDeclaration(
/*decorators*/ undefined,
/*modifiers*/ undefined,
name,
[newParameter],
body || factory.createBlock([])));
}
}
return nodes;
return createFunctionFromType(type, style);
}
default:
return undefined;
Expand Down