Skip to content

Commit 23226ae

Browse files
committed
add experimental fixSuggestionsSorting for object property access
1 parent 78db8e3 commit 23226ae

File tree

5 files changed

+44
-14
lines changed

5 files changed

+44
-14
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
"mocha": "^10.0.0",
8383
"modify-json-file": "^1.2.2",
8484
"pluralize": "github:plurals/pluralize#36f03cd2d573fa6d23e12e1529fa4627e2af74b4",
85+
"rambda": "^7.2.1",
8586
"require-from-string": "^2.0.2",
8687
"string-dedent": "^3.0.1",
8788
"vscode-framework": "^0.0.18"

pnpm-lock.yaml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/configurationType.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,17 @@ export type Configuration = {
8181
* */
8282
'highlightNonFunctionMethods.enable': boolean
8383
/**
84-
* Use originl sorting of suggestions (almost like in WebStorm). Works only with TypeScript <= 4.5.5
84+
* Normalize sorting of suggestions after plugin modifications
85+
* You most probably don't need to disable it
8586
* @default true
8687
* */
8788
'correctSorting.enable': boolean
89+
/**
90+
* Try to restore suggestion sorting after `.`
91+
* Experimental and most probably will be changed in future
92+
* @default false
93+
*/
94+
fixSuggestionsSorting: boolean
8895
// TODO
8996
/**
9097
* Mark QuickFixes & refactorings with 🔵
Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
1-
import type tslib from 'typescript/lib/tsserverlibrary'
1+
import { oneOf } from '@zardoy/utils'
2+
import { groupBy, partition } from 'rambda'
23

3-
export default (
4-
position: number,
5-
node: tslib.Node | undefined,
6-
scriptSnapshot: tslib.IScriptSnapshot,
7-
sourceFile: tslib.SourceFile,
8-
program: tslib.Program,
9-
ts: typeof tslib,
10-
) => {
4+
export default (entries: ts.CompletionEntry[], node: ts.Node | undefined, sourceFile: ts.SourceFile, program: ts.Program) => {
115
if (!node) return
12-
// TO BE DONE HERE
13-
// const typeChecker = program.getTypeChecker()
14-
// const type = typeChecker.getTypeAtLocation(node)
15-
// console.log(typeChecker.getAugmentedPropertiesOfType(type).map(({ name }) => name))
6+
// if (ts.isObjectLiteralExpression(node) && ts.isCallExpression(node.parent)) {
7+
// const typeChecker = program.getTypeChecker()
8+
// const type = typeChecker.getTypeAtLocation(node.parent)
9+
// const callSignatures = type.getCallSignatures()
10+
// }
11+
if (ts.isIdentifier(node)) node = node.parent
12+
if (!ts.isPropertyAccessExpression(node)) return
13+
const typeChecker = program.getTypeChecker()
14+
const expr = node.expression
15+
const type = typeChecker.getTypeAtLocation(expr)
16+
const sourceProps = type.getProperties?.()?.map(({ name }) => name)
17+
// languageService.getSignatureHelpItems(fileName, position, {}))
18+
if (!sourceProps) return
19+
// const entriesBySortText = groupBy(({ sortText }) => sortText, entries)
20+
const [interestedEntries, notInterestedEntries] = partition(
21+
entry => oneOf(entry.kind, ts.ScriptElementKind.memberVariableElement, ts.ScriptElementKind.memberFunctionElement),
22+
entries,
23+
)
24+
// if sortText first symbol is not a number, than most probably it was highlighted by IntelliCode, keep them high
25+
const [sortableEntries, notSortableEntries] = partition(entry => !isNaN(+entry.sortText), interestedEntries)
26+
const lowestSortText = Math.min(...sortableEntries.map(({ sortText }) => +sortText))
27+
// make sorted
28+
sortableEntries
29+
.sort((a, b) => {
30+
return sourceProps.indexOf(a.name) - sourceProps.indexOf(b.name)
31+
})
32+
.map((entry, i) => ({ ...entry, sortText: String(lowestSortText + i) }))
33+
return [...notSortableEntries, ...sortableEntries, ...notInterestedEntries]
1634
}

typescript/src/completionsAtPosition.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ export const getCompletionsAtPosition = (
132132

133133
if (!prior) return
134134

135+
if (c('fixSuggestionsSorting')) prior.entries = fixPropertiesSorting(prior.entries, leftNode, sourceFile, program) ?? prior.entries
136+
135137
const entryNames = new Set(prior.entries.map(({ name }) => name))
136138
if (c('removeUselessFunctionProps.enable')) prior.entries = prior.entries.filter(e => !['Symbol', 'caller', 'prototype'].includes(e.name))
137139
if (['bind', 'call', 'caller'].every(name => entryNames.has(name)) && c('highlightNonFunctionMethods.enable')) {

0 commit comments

Comments
 (0)