Skip to content

Commit a7d67f1

Browse files
committed
feat: special command: removeFunctionArgumentsTypesInSelection
1 parent eb41a22 commit a7d67f1

File tree

6 files changed

+97
-2
lines changed

6 files changed

+97
-2
lines changed

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
"command": "enableStrictEmmetInJsx",
1212
"title": "Enable Strict Emmet in JSX",
1313
"category": "TS Essentials JSX"
14+
},
15+
{
16+
"command": "removeFunctionArgumentsTypesInSelection",
17+
"title": "Remove Function Arguments Types in Selection",
18+
"category": "TS Essentials"
1419
}
1520
],
1621
"typescriptServerPlugins": [

src/extension.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import migrateSettings from './migrateSettings'
1212
import figIntegration from './figIntegration'
1313
import apiCommands from './apiCommands'
1414
import onCompletionAccepted from './onCompletionAccepted'
15+
import specialCommands from './specialCommands'
1516

1617
export const activateTsPlugin = (tsApi: { configurePlugin; onCompletionAccepted }) => {
1718
let webWaitingForConfigSync = false
@@ -66,6 +67,7 @@ export const activateTsPlugin = (tsApi: { configurePlugin; onCompletionAccepted
6667
void registerEmmet()
6768
webImports()
6869
apiCommands()
70+
specialCommands()
6971

7072
figIntegration()
7173
}

src/sendCommand.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@ import { TriggerCharacterCommand } from '../typescript/src/ipcTypes'
66
type SendCommandData = {
77
position: vscode.Position
88
document: vscode.TextDocument
9+
inputOptions?: any
910
}
1011
export const sendCommand = async <T>(command: TriggerCharacterCommand, sendCommandDataArg?: SendCommandData): Promise<T | undefined> => {
1112
// plugin id disabled, languageService would not understand the special trigger character
1213
if (!getExtensionSetting('enablePlugin')) return
1314

15+
if (sendCommandDataArg?.inputOptions) {
16+
command = `${command}?${JSON.stringify(sendCommandDataArg.inputOptions)}` as any
17+
}
18+
1419
const {
1520
document: { uri },
1621
position,

src/specialCommands.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import * as vscode from 'vscode'
2+
import { getActiveRegularEditor } from '@zardoy/vscode-utils'
3+
import { registerExtensionCommand } from 'vscode-framework'
4+
import { RequestOptionsTypes, RequestResponseTypes } from '../typescript/src/ipcTypes'
5+
import { sendCommand } from './sendCommand'
6+
7+
export default () => {
8+
registerExtensionCommand('removeFunctionArgumentsTypesInSelection', async () => {
9+
const editor = getActiveRegularEditor()
10+
if (!editor) return
11+
const { selection, document } = editor
12+
const response = await sendCommand<RequestResponseTypes['removeFunctionArgumentsTypesInSelection']>('removeFunctionArgumentsTypesInSelection', {
13+
document,
14+
position: selection.start,
15+
inputOptions: {
16+
endSelection: document.offsetAt(selection.end),
17+
} as RequestOptionsTypes['removeFunctionArgumentsTypesInSelection'],
18+
})
19+
if (!response) return
20+
const { ranges } = response
21+
void editor.edit(builder => {
22+
for (const [start, end] of ranges) {
23+
builder.delete(new vscode.Range(document.positionAt(start), document.positionAt(end)))
24+
}
25+
})
26+
})
27+
28+
registerExtensionCommand('pickAndInsertFunctionArguments', async () => {
29+
const editor = getActiveRegularEditor()
30+
if (!editor) return
31+
const result = await sendCommand<RequestResponseTypes['pickAndInsertFunctionArguments']>('pickAndInsertFunctionArguments')
32+
if (!result) return
33+
result.functions
34+
})
35+
}

typescript/src/ipcTypes.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
export const passthroughExposedApiCommands = ['getNodePath', 'getSpanOfEnclosingComment', 'getNodeAtPosition'] as const
22

3-
export const triggerCharacterCommands = [...passthroughExposedApiCommands, 'emmet-completions', 'getPostfixes'] as const
3+
export const triggerCharacterCommands = [
4+
...passthroughExposedApiCommands,
5+
'emmet-completions',
6+
'getPostfixes',
7+
'filterBySyntaxKind',
8+
'removeFunctionArgumentsTypesInSelection',
9+
'pickAndInsertFunctionArguments',
10+
] as const
411

512
export type TriggerCharacterCommand = typeof triggerCharacterCommands[number]
613

@@ -10,6 +17,21 @@ export type NodeAtPositionResponse = {
1017
end: number
1118
}
1219

20+
export type RequestResponseTypes = {
21+
removeFunctionArgumentsTypesInSelection: {
22+
ranges: [number, number][]
23+
}
24+
pickAndInsertFunctionArguments: {
25+
functions: string[]
26+
}
27+
}
28+
29+
export type RequestOptionsTypes = {
30+
removeFunctionArgumentsTypesInSelection: {
31+
endSelection: number
32+
}
33+
}
34+
1335
// export type EmmetResult = {
1436
// label: string
1537
// documentation: any

typescript/src/specialCommands/handle.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import postfixesAtPosition from '../completions/postfixesAtPosition'
2-
import { NodeAtPositionResponse, TriggerCharacterCommand, triggerCharacterCommands } from '../ipcTypes'
2+
import { NodeAtPositionResponse, RequestOptionsTypes, TriggerCharacterCommand, triggerCharacterCommands } from '../ipcTypes'
33
import { findChildContainingPosition, getNodePath } from '../utils'
44
import getEmmetCompletions from './emmet'
55

@@ -14,6 +14,9 @@ export default (
1414
entries: []
1515
typescriptEssentialsResponse: any
1616
} => {
17+
const _specialCommandsParts = specialCommand.split('?')
18+
specialCommand = _specialCommandsParts[0]! as TriggerCharacterCommand
19+
const specialCommandArg = _specialCommandsParts[1] && JSON.parse(_specialCommandsParts[1])
1720
if (triggerCharacterCommands.includes(specialCommand) && !configuration) {
1821
throw new Error('no-ts-essential-plugin-configuration')
1922
}
@@ -54,8 +57,31 @@ export default (
5457
typescriptEssentialsResponse: postfixesAtPosition(position, fileName, scriptSnapshot, info.languageService),
5558
} as any
5659
}
60+
if (specialCommand === 'removeFunctionArgumentsTypesInSelection') {
61+
changeType<RequestOptionsTypes['removeFunctionArgumentsTypesInSelection']>(specialCommandArg)
62+
63+
const node = findChildContainingPosition(ts, sourceFile, position)
64+
if (!node) return
65+
if (!ts.isIdentifier(node) || !node.parent || !ts.isParameter(node.parent) || !node.parent.parent?.parameters) {
66+
return
67+
}
68+
const allParams = node.parent.parent.parameters
69+
return {
70+
entries: [],
71+
typescriptEssentialsResponse: {
72+
ranges: allParams
73+
.map(param => {
74+
if (!param.type || param.name.pos > specialCommandArg.endSelection) return
75+
return [param.name.end, param.type.end]
76+
})
77+
.filter(Boolean),
78+
},
79+
}
80+
}
5781
}
5882

83+
function changeType<T>(arg): asserts arg is T {}
84+
5985
function nodeToApiResponse(node: ts.Node): NodeAtPositionResponse {
6086
return {
6187
kindName: ts.SyntaxKind[node.kind]!,

0 commit comments

Comments
 (0)