Skip to content

Commit 28ea606

Browse files
author
Orta Therox
authored
Merge pull request #2105 from orta/inlays
Adds support for showing queries using inlay hints if it is available
2 parents d83b3b6 + ba72e49 commit 28ea606

File tree

8 files changed

+68
-19
lines changed

8 files changed

+68
-19
lines changed

packages/create-typescript-playground-plugin/template/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"@rollup/plugin-typescript": "^3.0.0",
2727
"@types/react": "^16.9.23",
2828
"concurrently": "^5.1.0",
29-
"monaco-editor": "^0.19.3",
29+
"monaco-editor": "^0.29.1",
3030
"node-fetch": "^2.6.0",
3131
"rollup": "^1.31.0",
3232
"serve": "^11.3.0",

packages/playground-worker/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ const worker: import("./types").CustomTSWebWorkerFactory = (TypeScriptWorker, ts
108108
}
109109

110110
// This is TypeScript asking 'whats the content of this file' - we want
111-
// to override the underlaying TS vfs model with our twoslash multi-file
111+
// to override the underlying TS vfs model with our twoslash multi-file
112112
// files when possible, otherwise pass it back to super
113113
override _getScriptText(fileName: string): string | undefined {
114114
const twoslashed = this.twoslashFiles.find(f => fileName === f.file)
@@ -168,8 +168,8 @@ const worker: import("./types").CustomTSWebWorkerFactory = (TypeScriptWorker, ts
168168

169169
override async getCompletionsAtPosition(fileName: string, position: number) {
170170
const empty = Promise.resolve({ isGlobalCompletion: false, isMemberCompletion: false, isNewIdentifierLocation: false, entries: [] })
171-
const complet = await this._overrideFileNamePos(super.getCompletionsAtPosition.bind(this), fileName, position, undefined, empty, (result) => result)
172-
return complet
171+
const completions = await this._overrideFileNamePos(super.getCompletionsAtPosition.bind(this), fileName, position, undefined, empty, (result) => result)
172+
return completions
173173
}
174174

175175
override async getCompletionEntryDetails(fileName: string, position: number, entry: string) {

packages/playground/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"devDependencies": {
2323
"@types/jest": "^25.1.3",
2424
"jest": "*",
25-
"monaco-editor": "~0.20.0",
25+
"monaco-editor": "^0.29.1",
2626
"monaco-typescript": "^3.7.0",
2727
"typescript": "*"
2828
}

packages/playground/src/index.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { createUtils, PluginUtils } from "./pluginUtils"
2424
import type React from "react"
2525
import { settingsPlugin, getPlaygroundPlugins } from "./sidebar/settings"
2626
import { gistPoweredNavBar } from "./navigation"
27+
import { createTwoslashInlayProvider } from "./twoslashInlays"
2728

2829
export { PluginUtils } from "./pluginUtils"
2930

@@ -78,8 +79,8 @@ export const setupPlayground = (
7879
react: typeof React
7980
) => {
8081
const playgroundParent = sandbox.getDomNode().parentElement!.parentElement!.parentElement!
81-
// UI to the left
8282

83+
// UI to the left
8384
const leftNav = createNavigationSection()
8485
playgroundParent.insertBefore(leftNav, sandbox.getDomNode().parentElement!.parentElement!)
8586

@@ -224,15 +225,17 @@ export const setupPlayground = (
224225

225226
// Add an outer package.json with 'module: type' and ensures all the
226227
// other settings are inline for ESM mode
227-
const moduleNumber = sandbox.getCompilerOptions().module as number || 0
228+
const moduleNumber = (sandbox.getCompilerOptions().module as number) || 0
228229
const isESMviaModule = moduleNumber > 99 && moduleNumber < 200
229230
const moduleResNumber = sandbox.getCompilerOptions().moduleResolution || 0
230231
const isESMviaModuleRes = moduleResNumber > 2 && moduleResNumber < 100
231232

232233
if (isESMviaModule || isESMviaModuleRes) {
233234
if (isESMMode) return
234235
isESMMode = true
235-
setTimeout(() => { ui.flashInfo(i("play_esm_mode")) }, 300)
236+
setTimeout(() => {
237+
ui.flashInfo(i("play_esm_mode"))
238+
}, 300)
236239

237240
const nextRes = moduleNumber === 199 ? 99 : 2
238241
sandbox.setCompilerSettings({ target: 99, moduleResolution: nextRes })
@@ -655,6 +658,10 @@ export const setupPlayground = (
655658
}
656659
}
657660

661+
if (monaco.languages.registerInlayHintsProvider) {
662+
monaco.languages.registerInlayHintsProvider(sandbox.language, createTwoslashInlayProvider(sandbox))
663+
}
664+
658665
if (location.hash.startsWith("#show-examples")) {
659666
setTimeout(() => {
660667
document.getElementById("examples-button")?.click()
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { Sandbox } from "typescriptlang-org/static/js/sandbox"
2+
3+
export const createTwoslashInlayProvider = (sandbox: Sandbox) => {
4+
const provider: import("monaco-editor").languages.InlayHintsProvider = {
5+
provideInlayHints: async (model, _, cancel) => {
6+
const text = model.getValue()
7+
const queryRegex = /^\s*\/\/\s*\^\?$/gm
8+
let match
9+
const results: import("monaco-editor").languages.InlayHint[] = []
10+
const worker = await sandbox.getWorkerProcess()
11+
if (model.isDisposed()) {
12+
return []
13+
}
14+
15+
while ((match = queryRegex.exec(text)) !== null) {
16+
const end = match.index + match[0].length - 1
17+
const endPos = model.getPositionAt(end)
18+
const inspectionPos = new sandbox.monaco.Position(endPos.lineNumber - 1, endPos.column)
19+
const inspectionOff = model.getOffsetAt(inspectionPos)
20+
21+
if (cancel.isCancellationRequested) return []
22+
23+
const hint = await worker.getQuickInfoAtPosition("file://" + model.uri.path, inspectionOff)
24+
if (!hint || !hint.displayParts) continue
25+
26+
const inlay: import("monaco-editor").languages.InlayHint = {
27+
// @ts-ignore
28+
kind: 0,
29+
position: new sandbox.monaco.Position(endPos.lineNumber, endPos.column + 1),
30+
text: hint.displayParts.map(d => d.text).join(""),
31+
whitespaceBefore: true,
32+
}
33+
results.push(inlay)
34+
}
35+
return results
36+
},
37+
}
38+
return provider
39+
}

packages/sandbox/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"devDependencies": {
2929
"@types/jest": "^25.1.3",
3030
"jest": "*",
31-
"monaco-editor": "~0.20.0",
31+
"monaco-editor": "^0.29.1",
3232
"monaco-typescript": "^3.7.0",
3333
"ts-jest": "^26.4.4",
3434
"typescript": "*"

packages/sandbox/src/index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ export type SandboxConfig = {
4848
groupEnd: (...args: any[]) => void
4949
}
5050
} & (
51-
| { /** the ID of a dom node to add monaco to */ domID: string }
52-
| { /** the dom node to add monaco to */ elementToAppend: HTMLElement }
53-
)
51+
| { /** the ID of a dom node to add monaco to */ domID: string }
52+
| { /** the dom node to add monaco to */ elementToAppend: HTMLElement }
53+
)
5454

5555
const languageType = (config: SandboxConfig) => (config.filetype === "js" ? "javascript" : "typescript")
5656

@@ -77,6 +77,9 @@ const sharedEditorOptions: import("monaco-editor").editor.IEditorOptions = {
7777
acceptSuggestionOnCommitCharacter: !isAndroid,
7878
acceptSuggestionOnEnter: !isAndroid ? "on" : "off",
7979
accessibilitySupport: !isAndroid ? "on" : "off",
80+
inlayHints: {
81+
enabled: true,
82+
},
8083
}
8184

8285
/** The default settings which we apply a partial over */
@@ -244,7 +247,7 @@ export const createTypeScriptSandbox = (
244247
defaults.setCompilerOptions(compilerOptions)
245248

246249
// To let clients plug into compiler settings changes
247-
let didUpdateCompilerSettings = (opts: CompilerOptions) => { }
250+
let didUpdateCompilerSettings = (opts: CompilerOptions) => {}
248251

249252
const updateCompilerSettings = (opts: CompilerOptions) => {
250253
const newKeys = Object.keys(opts)

yarn.lock

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5550,7 +5550,7 @@ __metadata:
55505550
"@typescript/sandbox": 0.1.0
55515551
esbuild: ^0.13.4
55525552
jest: "*"
5553-
monaco-editor: ~0.20.0
5553+
monaco-editor: ^0.29.1
55545554
monaco-typescript: ^3.7.0
55555555
typescript: "*"
55565556
languageName: unknown
@@ -5564,7 +5564,7 @@ __metadata:
55645564
"@typescript/ata": 0.9.2
55655565
"@typescript/vfs": 1.3.5
55665566
jest: "*"
5567-
monaco-editor: ~0.20.0
5567+
monaco-editor: ^0.29.1
55685568
monaco-typescript: ^3.7.0
55695569
ts-jest: ^26.4.4
55705570
typescript: "*"
@@ -20172,10 +20172,10 @@ fsevents@^1.2.7:
2017220172
languageName: node
2017320173
linkType: hard
2017420174

20175-
"monaco-editor@npm:~0.20.0":
20176-
version: 0.20.0
20177-
resolution: "monaco-editor@npm:0.20.0"
20178-
checksum: a7c7bf738bb8625812d40447be2fc085652fe40611a1a161f12983823d54f2d53b4ed5cf2b42841d8099ce3b536bd042fb6b781acce55a7183b99aa743a7ca39
20175+
"monaco-editor@npm:^0.29.1":
20176+
version: 0.29.1
20177+
resolution: "monaco-editor@npm:0.29.1"
20178+
checksum: bc1df4f0c026073fdfc5b25487c58eabc82b7341653e75e5a5fa06c7754f823d0751d1eb5f4fb1cc4a0d5d0f1ca801b724b3997e0b5acc942e68358be9d70b71
2017920179
languageName: node
2018020180
linkType: hard
2018120181

0 commit comments

Comments
 (0)