Skip to content

Commit c63f706

Browse files
committed
feat(demo): add fig unreleased integration
1 parent 91df7a6 commit c63f706

File tree

3 files changed

+55
-6
lines changed

3 files changed

+55
-6
lines changed

src/configurationType.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,4 +275,12 @@ export type Configuration = {
275275
}
276276
| false
277277
}
278+
/**
279+
* The integration is enabled, only when this array is not empty
280+
* Integration supports only string within function call
281+
* Examples: `cp.exec(`, `executeShellCommand(`
282+
* @uniqueItems
283+
* @default []
284+
*/
285+
'figIntegration.enableWhenStartsWith': string[]
278286
}

src/extension.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { sendCommand } from './sendCommand'
1111
import { registerEmmet } from './emmet'
1212
import experimentalPostfixes from './experimentalPostfixes'
1313
import migrateSettings from './migrateSettings'
14+
import figIntegration from './figIntegration'
1415

1516
export const activateTsPlugin = (tsApi: { configurePlugin; onCompletionAccepted }) => {
1617
let webWaitingForConfigSync = false
@@ -113,12 +114,7 @@ export const activateTsPlugin = (tsApi: { configurePlugin; onCompletionAccepted
113114
void registerEmmet()
114115
webImports()
115116

116-
// registerActiveDevelopmentCommand(async () => {
117-
// const items: vscode.DocumentSymbol[] = await vscode.commands.executeCommand(
118-
// 'vscode.executeDocumentSymbolProvider',
119-
// vscode.Uri.file(...),
120-
// )
121-
// })
117+
void figIntegration()
122118
}
123119

124120
export const activate = async () => {

src/figIntegration.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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

Comments
 (0)