Skip to content

Commit 60326d0

Browse files
committed
cleanup non-function-methods, add screenshot
fixes #110
1 parent 20ee5fe commit 60326d0

File tree

4 files changed

+37
-27
lines changed

4 files changed

+37
-27
lines changed

media/non-function-methods.png

48.8 KB
Loading

src/configurationType.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export type Configuration = {
143143
*/
144144
'arrayMethodsSnippets.defaultItemName': string | false
145145
/**
146-
* Highlight and lift non-function methods. Also applies for static class methods. Uses `bind`, `call`, `caller` detection.
146+
* Highlights non-function methods. Also applies for static class methods. Activates when `bind`, `call`, `caller` completions detected.
147147
* @default true
148148
* */
149149
'highlightNonFunctionMethods.enable': boolean
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { oneOf } from '@zardoy/utils'
2+
import { sharedCompletionContext } from './sharedContext'
3+
4+
export default (entries: ts.CompletionEntry[]) => {
5+
const { c, prevCompletionsMap } = sharedCompletionContext
6+
7+
if (c('removeUselessFunctionProps.enable')) {
8+
entries = entries.filter(entry => {
9+
if (oneOf(entry.kind, ts.ScriptElementKind.warning)) return true
10+
return !['Symbol', 'caller', 'prototype'].includes(entry.name)
11+
})
12+
}
13+
14+
const entryNames = new Set(entries.map(({ name, kind }) => (kind === ts.ScriptElementKind.warning ? '' : name)))
15+
if (['bind', 'call', 'caller'].every(name => entryNames.has(name)) && c('highlightNonFunctionMethods.enable')) {
16+
const standardProps = new Set(['Symbol', 'apply', 'arguments', 'bind', 'call', 'caller', 'length', 'name', 'prototype', 'toString'])
17+
entries = entries.map(entry => {
18+
if (!standardProps.has(entry.name) && entry.kind !== ts.ScriptElementKind.warning) {
19+
const newName = `☆${entry.name}`
20+
prevCompletionsMap[newName] = {
21+
originalName: entry.name,
22+
}
23+
return {
24+
...entry,
25+
insertText: entry.insertText ?? entry.name,
26+
name: newName,
27+
}
28+
}
29+
30+
return entry
31+
})
32+
}
33+
return entries
34+
}

typescript/src/completionsAtPosition.ts

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import addSourceDefinition from './completions/addSourceDefinition'
2727
import { sharedCompletionContext } from './completions/sharedContext'
2828
import displayImportedInfo from './completions/displayImportedInfo'
2929
import changeKindToFunction from './completions/changeKindToFunction'
30+
import functionPropsAndMethods from './completions/functionPropsAndMethods'
3031

3132
export type PrevCompletionMap = Record<
3233
string,
@@ -183,32 +184,7 @@ export const getCompletionsAtPosition = (
183184
prior.entries = fixPropertiesSorting(prior.entries) ?? prior.entries
184185
if (node) prior.entries = boostKeywordSuggestions(prior.entries, position, node) ?? prior.entries
185186

186-
const entryNames = new Set(prior.entries.map(({ name }) => name))
187-
if (c('removeUselessFunctionProps.enable')) {
188-
prior.entries = prior.entries.filter(entry => {
189-
if (oneOf(entry.kind, ts.ScriptElementKind.warning)) return true
190-
return !['Symbol', 'caller', 'prototype'].includes(entry.name)
191-
})
192-
}
193-
if (['bind', 'call', 'caller'].every(name => entryNames.has(name)) && c('highlightNonFunctionMethods.enable')) {
194-
const standardProps = new Set(['Symbol', 'apply', 'arguments', 'bind', 'call', 'caller', 'length', 'name', 'prototype', 'toString'])
195-
// TODO lift up!
196-
prior.entries = prior.entries.map(entry => {
197-
if (!standardProps.has(entry.name) && entry.kind !== ts.ScriptElementKind.warning) {
198-
const newName = `☆${entry.name}`
199-
prevCompletionsMap[newName] = {
200-
originalName: entry.name,
201-
}
202-
return {
203-
...entry,
204-
insertText: entry.insertText ?? entry.name,
205-
name: newName,
206-
}
207-
}
208-
209-
return entry
210-
})
211-
}
187+
prior.entries = functionPropsAndMethods(prior.entries)
212188

213189
// if (c('completionHelpers') && node) prior.entries = objectLiteralHelpers(node, prior.entries) ?? prior.entries
214190

0 commit comments

Comments
 (0)