Skip to content

Commit 2c1c288

Browse files
authored
Support two slash queries (#298)
* update vscode version * support twoslash-queries Same as https://github.com/orta/vscode-twoslash-queries but in *.vue files
1 parent 210b8be commit 2c1c288

File tree

5 files changed

+90
-6
lines changed

5 files changed

+90
-6
lines changed

extensions/vscode-vue-language-features/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
"author": "Rahul Kadyan <[email protected]> (https://znck.me/)",
5555
"license": "MIT",
5656
"engines": {
57-
"vscode": "^1.63.0"
57+
"vscode": "^1.67.0"
5858
},
5959
"categories": [
6060
"Programming Languages"
@@ -329,7 +329,7 @@
329329
},
330330
"devDependencies": {
331331
"@types/node": "^10.12.0",
332-
"@types/vscode": "^1.63.0",
332+
"@types/vscode": "^1.67.0",
333333
"typescript": "^4.6.3",
334334
"vsce": "2.6.7"
335335
},

extensions/vscode-vue-language-features/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { VueVirtualDocumentProvider } from './scheme/vue'
1111
import { PluginCommunicationService } from './services/PluginCommunicationService'
1212
import { StyleLanguageProxy } from './services/StyleLanguageProxy'
1313
import { TemplateLanguageProxy } from './services/TemplateLanguageProxy'
14+
import { TwoSlashService } from './services/TwoSlashService'
1415
import { VirtualFileSwitcher } from './services/VirtualFileSwitcher'
1516

1617
let client: LanguageClient | undefined
@@ -34,6 +35,7 @@ export async function activate(
3435
container.get(OpenVirtualFileCommand).install(),
3536
container.get(SelectVirtualFileCommand).install(),
3637
container.get(VirtualFileSwitcher).install(),
38+
container.get(TwoSlashService).install(),
3739
new vscode.Disposable(() => container.unbindAll()),
3840
)
3941
const ts = vscode.extensions.getExtension(
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { injectable } from 'inversify'
2+
import vscode, { Disposable, InlayHint, InlayHintsProvider } from 'vscode'
3+
import { Installable } from '../utils/installable'
4+
5+
@injectable()
6+
export class TwoSlashService
7+
extends Installable
8+
implements InlayHintsProvider<InlayHint>
9+
{
10+
public install(): Disposable {
11+
super.install()
12+
13+
return vscode.languages.registerInlayHintsProvider(
14+
{ language: 'vue' },
15+
this,
16+
)
17+
}
18+
19+
async provideInlayHints(
20+
document: vscode.TextDocument,
21+
range: vscode.Range,
22+
token: vscode.CancellationToken,
23+
): Promise<InlayHint[]> {
24+
const source = document.getText(range)
25+
const matches = source.matchAll(/^\s*(\/\/|<!--|\/\*)\s*\^\?/gm)
26+
27+
const offset = document.offsetAt(range.start)
28+
const file =
29+
document.uri.scheme === 'file'
30+
? document.uri.fsPath
31+
: `^/${document.uri.scheme}/${
32+
document.uri.authority ?? 'ts-nul-authority'
33+
}/${document.uri.path.replace(/^\//, '')}`
34+
const hints: InlayHint[] = []
35+
for (const match of matches) {
36+
if (match.index == null) continue
37+
if (match[0] == null) continue
38+
39+
if (token.isCancellationRequested) return []
40+
41+
const end = match.index + match[0].length - 1
42+
const position = document.positionAt(offset + end)
43+
const inspectionPosition = new vscode.Position(
44+
position.line - 1,
45+
position.character,
46+
)
47+
const hint: any = await vscode.commands.executeCommand(
48+
'typescript.tsserverRequest',
49+
'quickinfo',
50+
{
51+
_: '%%%',
52+
file,
53+
line: inspectionPosition.line + 1,
54+
offset: inspectionPosition.character,
55+
},
56+
)
57+
58+
if (hint == null || hint.body == null) continue
59+
let label: string = hint.body.displayString
60+
.replace(/\\n/g, ' ')
61+
.replace(/\/n/g, ' ')
62+
.replace(/ {2}/g, ' ')
63+
// eslint-disable-next-line no-control-regex
64+
.replace(/[\u0000-\u001F\u007F-\u009F]/g, '')
65+
if (label.length > 120) {
66+
label = `${label.slice(0, 119)}...`
67+
}
68+
69+
hints.push({
70+
kind: 0,
71+
position: new vscode.Position(position.line, position.character + 1),
72+
label,
73+
paddingLeft: true,
74+
})
75+
}
76+
77+
return hints
78+
}
79+
}

pnpm-lock.yaml

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tsconfig.base.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@
2323
"strictNullChecks": true,
2424
"strictPropertyInitialization": true,
2525

26-
2726
"emitDecoratorMetadata": true,
2827
"experimentalDecorators": true,
2928

3029
"resolveJsonModule": true,
3130

32-
"lib": ["ES2019"]
31+
"lib": ["ES2020"]
3332
}
3433
}

0 commit comments

Comments
 (0)