Skip to content

Commit 3a73e51

Browse files
committed
feat: Add API vscode command tsEssentialPlugins.getNodeAtPosition that some extension can use to reliably get node at position!
1 parent 36da1ed commit 3a73e51

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

src/extension.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import * as vscode from 'vscode'
33
import { defaultJsSupersetLangs } from '@zardoy/vscode-utils/build/langs'
44
import { getActiveRegularEditor } from '@zardoy/vscode-utils'
5-
import { getExtensionSetting, registerActiveDevelopmentCommand, extensionCtx, getExtensionSettingId } from 'vscode-framework'
5+
import { getExtensionSetting, extensionCtx, getExtensionSettingId, getExtensionCommandId } from 'vscode-framework'
66
import { pickObj } from '@zardoy/utils'
77
import throttle from 'lodash.throttle'
88
import { PostfixCompletion, TriggerCharacterCommand } from '../typescript/src/ipcTypes'
@@ -149,6 +149,18 @@ export const activate = async () => {
149149
'.',
150150
)
151151

152+
type RequestOptions = Partial<{
153+
offset: number
154+
}>
155+
vscode.commands.registerCommand(getExtensionCommandId('getNodeAtPosition' as never), async (_, { offset }: RequestOptions = {}) => {
156+
const { activeTextEditor } = vscode.window
157+
if (!activeTextEditor) return
158+
const { document } = activeTextEditor
159+
const { typescriptEssentialsResponse: data } =
160+
(await sendCommand('nodeAtPosition', { document, position: offset ? document.positionAt(offset) : activeTextEditor.selection.active })) ?? {}
161+
return data
162+
})
163+
152164
// not removing as we can enable it back in near future
153165
const enableExperimentalPluginRestoration = false
154166

typescript/src/ipcTypes.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
export const triggerCharacterCommands = ['find-in-import', 'getPostfixes', 'check-configuration'] as const
1+
export const triggerCharacterCommands = ['find-in-import', 'getPostfixes', 'nodeAtPosition', 'check-configuration'] as const
22
export type TriggerCharacterCommand = typeof triggerCharacterCommands[number]
33

4+
export type NodeAtPositionResponse = {
5+
kindName: string
6+
start: number
7+
end: number
8+
}
9+
410
export type PostfixCompletion = {
511
label: string
612
// replacement: [startOffset: number, endOffset?: number]

typescript/src/specialCommands/handle.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
import type tslib from 'typescript/lib/tsserverlibrary'
22
import postfixesAtPosition from '../completions/postfixesAtPosition'
3-
import { TriggerCharacterCommand, triggerCharacterCommands } from '../ipcTypes'
3+
import { NodeAtPositionResponse, TriggerCharacterCommand, triggerCharacterCommands } from '../ipcTypes'
4+
import { findChildContainingPosition } from '../utils'
45

5-
export default (info: tslib.server.PluginCreateInfo, fileName: string, position: number, specialCommand: TriggerCharacterCommand, configuration: any) => {
6+
export default (
7+
info: tslib.server.PluginCreateInfo,
8+
fileName: string,
9+
position: number,
10+
specialCommand: TriggerCharacterCommand,
11+
configuration: any,
12+
): void | {
13+
entries: []
14+
typescriptEssentialsResponse: any
15+
} => {
616
if (specialCommand === 'check-configuration') {
717
return {
818
entries: [],
@@ -12,6 +22,19 @@ export default (info: tslib.server.PluginCreateInfo, fileName: string, position:
1222
if (triggerCharacterCommands.includes(specialCommand) && !configuration) {
1323
throw new Error('no-ts-essential-plugin-configuration')
1424
}
25+
if (specialCommand === 'nodeAtPosition') {
26+
const node = findChildContainingPosition(ts, info.languageService.getProgram()!.getSourceFile(fileName)!, position)
27+
return {
28+
entries: [],
29+
typescriptEssentialsResponse: !node
30+
? undefined
31+
: ({
32+
kindName: ts.SyntaxKind[node.kind],
33+
start: node.getStart(),
34+
end: node.getEnd(),
35+
} as NodeAtPositionResponse),
36+
}
37+
}
1538
if (specialCommand === 'getPostfixes') {
1639
const scriptSnapshot = info.project.getScriptSnapshot(fileName)
1740
if (!scriptSnapshot) return

0 commit comments

Comments
 (0)