Skip to content

Commit bee60ef

Browse files
committed
fix: improve a bit Volar takeover mode support
1 parent d23f029 commit bee60ef

File tree

2 files changed

+59
-16
lines changed

2 files changed

+59
-16
lines changed

src/extension.ts

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/* eslint-disable @typescript-eslint/no-require-imports */
22
import * as vscode from 'vscode'
33
import { defaultJsSupersetLangs } from '@zardoy/vscode-utils/build/langs'
4-
import { extensionCtx, getExtensionSettingId } from 'vscode-framework'
4+
import { extensionCtx, getExtensionSetting, getExtensionSettingId } from 'vscode-framework'
55
import { pickObj } from '@zardoy/utils'
6+
import { watchExtensionSettings } from '@zardoy/vscode-utils/build/settings'
67
import { Configuration } from './configurationType'
78
import webImports from './webImports'
89
import { sendCommand } from './sendCommand'
@@ -15,10 +16,16 @@ import specialCommands from './specialCommands'
1516
import vueVolarSupport from './vueVolarSupport'
1617
import moreCompletions from './moreCompletions'
1718

18-
export const activateTsPlugin = (tsApi: { configurePlugin; onCompletionAccepted }) => {
19+
let isActivated = false
20+
// let erroredStatusBarItem: vscode.StatusBarItem | undefined
21+
22+
export const activateTsPlugin = (tsApi: { configurePlugin; onCompletionAccepted } | undefined) => {
23+
if (isActivated) return
24+
isActivated = true
1925
let webWaitingForConfigSync = false
2026

2127
const syncConfig = () => {
28+
if (!tsApi) return
2229
console.log('sending configure request for typescript-essential-plugins')
2330
const config = vscode.workspace.getConfiguration().get(process.env.IDS_PREFIX!)
2431

@@ -48,7 +55,7 @@ export const activateTsPlugin = (tsApi: { configurePlugin; onCompletionAccepted
4855
})
4956
syncConfig()
5057

51-
onCompletionAccepted(tsApi)
58+
if (tsApi) onCompletionAccepted(tsApi)
5259

5360
if (process.env.PLATFORM === 'web') {
5461
const possiblySyncConfig = async () => {
@@ -79,24 +86,47 @@ export const activate = async () => {
7986

8087
const possiblyActivateTsPlugin = async () => {
8188
const tsExtension = vscode.extensions.getExtension('vscode.typescript-language-features')
82-
if (!tsExtension) return
89+
if (tsExtension) {
90+
await tsExtension.activate()
91+
92+
if (!tsExtension.exports || !tsExtension.exports.getAPI) {
93+
throw new Error("TS extension doesn't export API")
94+
}
95+
96+
// Get the API from the TS extension
97+
const api = tsExtension.exports.getAPI(0)
98+
if (!api) {
99+
throw new Error("TS extension doesn't have API")
100+
}
83101

84-
await tsExtension.activate()
102+
activateTsPlugin(api)
103+
return true
104+
}
85105

86-
if (!tsExtension.exports || !tsExtension.exports.getAPI) return
106+
if (vscode.extensions.getExtension('Vue.volar') && getExtensionSetting('enableVueSupport')) {
107+
activateTsPlugin(undefined)
108+
return true
109+
}
87110

88-
// Get the API from the TS extension
89-
const api = tsExtension.exports.getAPI(0)
90-
if (!api) return
91-
activateTsPlugin(api)
92-
return true
111+
return false
93112
}
94113

95114
const isActivated = (await possiblyActivateTsPlugin()) ?? false
96115
if (!isActivated) {
97-
// can be also used in future, for now only when activating TS extension manually
98-
const { dispose } = vscode.extensions.onDidChange(async () => {
99-
if (await possiblyActivateTsPlugin()) dispose()
116+
// can be also used in future, for now only when activating TS or Volar extension manually
117+
const disposables = []
118+
const { dispose } = vscode.extensions.onDidChange(
119+
async () => {
120+
if (await possiblyActivateTsPlugin()) dispose()
121+
},
122+
undefined,
123+
disposables,
124+
)
125+
watchExtensionSettings(['enableVueSupport'], async () => {
126+
if (await possiblyActivateTsPlugin()) {
127+
// todo
128+
// disposables.forEach(d => d.dispose())
129+
}
100130
})
101131
}
102132
}

src/sendCommand.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as vscode from 'vscode'
22
import { getActiveRegularEditor } from '@zardoy/vscode-utils'
33
import { getExtensionSetting } from 'vscode-framework'
4-
import { TriggerCharacterCommand } from '../typescript/src/ipcTypes'
4+
import { passthroughExposedApiCommands, TriggerCharacterCommand } from '../typescript/src/ipcTypes'
55

66
type SendCommandData<K> = {
77
position: vscode.Position
@@ -10,7 +10,20 @@ type SendCommandData<K> = {
1010
}
1111
export const sendCommand = async <T, K = any>(command: TriggerCharacterCommand, sendCommandDataArg?: SendCommandData<K>): Promise<T | undefined> => {
1212
// plugin id disabled, languageService would not understand the special trigger character
13-
if (!getExtensionSetting('enablePlugin')) return
13+
if (!getExtensionSetting('enablePlugin')) {
14+
console.warn('Ignoring request because plugin is disabled')
15+
return
16+
}
17+
if (!vscode.extensions.getExtension('vscode.typescript-language-features')) {
18+
const message = 'Special commands are not supported in Volar takeover mode'
19+
if (passthroughExposedApiCommands.includes(command as any)) {
20+
// don't spam in case of api command
21+
console.error(message)
22+
} else {
23+
throw new Error(message)
24+
}
25+
return
26+
}
1427

1528
if (sendCommandDataArg?.inputOptions) {
1629
command = `${command}?${JSON.stringify(sendCommandDataArg.inputOptions)}` as any

0 commit comments

Comments
 (0)