|
| 1 | +import * as vscode from 'vscode' |
| 2 | +import { defaultJsSupersetLangs } from '@zardoy/vscode-utils/build/langs' |
| 3 | +import { getExtensionSetting } from 'vscode-framework' |
| 4 | + |
| 5 | +export default async () => { |
| 6 | + // todo I think would be good to support zx package $ |
| 7 | + const figExtension = vscode.extensions.getExtension('undefined_publisher.fig-unreleased') |
| 8 | + if (!figExtension) return |
| 9 | + const api = await figExtension.activate() |
| 10 | + |
| 11 | + api.registerLanguageSupport( |
| 12 | + // why include react langs? becuase of ink package! |
| 13 | + defaultJsSupersetLangs, |
| 14 | + { |
| 15 | + async provideSingleLineRangeFromPosition(doc: vscode.TextDocument, position: vscode.Position) { |
| 16 | + const enableWhenStartsWith = getExtensionSetting('figIntegration.enableWhenStartsWith') |
| 17 | + if (enableWhenStartsWith.length === 0) return |
| 18 | + |
| 19 | + const path: any[] = await vscode.commands.executeCommand('tsEssentialPlugins.getNodePath', { |
| 20 | + offset: doc.offsetAt(position.translate(0, -1)), |
| 21 | + }) |
| 22 | + if (!path) return |
| 23 | + const lastTwoPaths = path.slice(-2) |
| 24 | + const kinds = lastTwoPaths.map(({ kindName }) => kindName) |
| 25 | + if (kinds[0] !== 'CallExpression' || !['StringLiteral', 'FirstTemplateToken'].includes(kinds[1])) return |
| 26 | + // todo use info from ts server isntead |
| 27 | + const callExpr = lastTwoPaths[0] |
| 28 | + const stringNode = lastTwoPaths[1] |
| 29 | + const callExpressionText = doc.getText(new vscode.Range(doc.positionAt(callExpr.start), doc.positionAt(callExpr.end))) |
| 30 | + if (enableWhenStartsWith.every(str => !callExpressionText.startsWith(str))) return |
| 31 | + return new vscode.Range(doc.positionAt((stringNode.start as number) + 1), doc.positionAt(stringNode.end - 1)) |
| 32 | + }, |
| 33 | + }, |
| 34 | + { |
| 35 | + // don't collide with TS rename provider |
| 36 | + disableProviders: ['rename'], |
| 37 | + enableCompletionProvider: { |
| 38 | + processCompletions(completions) { |
| 39 | + // make it above vscode snippets, maybe should make it builtin? |
| 40 | + return completions.map(({ sortText = '', ...compl }) => ({ ...compl, sortText: `!${sortText}` })) |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + ) |
| 45 | +} |
0 commit comments