Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
chore(website): [playground] add twoslash queries#8119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
7 commits Select commitHold shift + click to select a range
84cdef0
chore(website): [playground] add twoslash queries
auvred8089ec8
Merge branch 'main' into chore/8055
auvred1b3178e
Update packages/website/src/components/editor/useSandboxServices.ts
auvred2e2f1de
Update packages/website/src/components/editor/createProvideTwoslashIn…
auvredb65a948
refactor: move twoslash regex closer to .exec
auvrede5130b4
refactor: return without declaring variable
auvred90d2e78
auvredFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
98 changes: 98 additions & 0 deletionspackages/website/src/components/editor/createProvideTwoslashInlay.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// The following code is adapted from the code in microsoft/TypeScript-Website. | ||
// Source: https://github.com/microsoft/TypeScript-Website/blob/c8b2ea8c8fc216c163fe293650b2666c8563a67d/packages/playground/src/twoslashInlays.ts | ||
// License: https://github.com/microsoft/TypeScript-Website/blob/c8b2ea8c8fc216c163fe293650b2666c8563a67d/LICENSE-CODE | ||
auvred marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
import type Monaco from 'monaco-editor'; | ||
import type * as ts from 'typescript'; | ||
import type { SandboxInstance } from './useSandboxServices'; | ||
function findTwoshashQueries(code: string): RegExpExecArray[] { | ||
let match: RegExpExecArray | null = null; | ||
const matches: RegExpExecArray[] = []; | ||
// RegExp that matches '^<spaces>//?<spaces>$' | ||
const twoslashQueryRegex = /^(\s*\/\/\s*\^\?)\s*$/gm; | ||
while ((match = twoslashQueryRegex.exec(code))) { | ||
matches.push(match); | ||
} | ||
return matches; | ||
} | ||
export function createTwoslashInlayProvider( | ||
sandbox: SandboxInstance, | ||
): Monaco.languages.InlayHintsProvider { | ||
return { | ||
provideInlayHints: async ( | ||
model, | ||
_, | ||
cancel, | ||
): Promise<Monaco.languages.InlayHintList> => { | ||
const worker = await sandbox.getWorkerProcess(); | ||
if (model.isDisposed() || cancel.isCancellationRequested) { | ||
return { | ||
hints: [], | ||
dispose(): void { | ||
/* nop */ | ||
}, | ||
}; | ||
} | ||
const queryMatches = findTwoshashQueries(model.getValue()); | ||
const results: Monaco.languages.InlayHint[] = []; | ||
for (const result of await Promise.all( | ||
queryMatches.map(q => resolveInlayHint(q)), | ||
)) { | ||
if (result) { | ||
results.push(result); | ||
} | ||
} | ||
return { | ||
hints: results, | ||
dispose(): void { | ||
/* nop */ | ||
}, | ||
}; | ||
async function resolveInlayHint( | ||
queryMatch: RegExpExecArray, | ||
): Promise<Monaco.languages.InlayHint | undefined> { | ||
const end = queryMatch.index + queryMatch[1].length - 1; | ||
const endPos = model.getPositionAt(end); | ||
const inspectionPos = new sandbox.monaco.Position( | ||
endPos.lineNumber - 1, | ||
endPos.column, | ||
); | ||
const inspectionOff = model.getOffsetAt(inspectionPos); | ||
const hint = await (worker.getQuickInfoAtPosition( | ||
'file://' + model.uri.path, | ||
inspectionOff, | ||
) as Promise<ts.QuickInfo | undefined>); | ||
if (!hint?.displayParts) { | ||
return; | ||
} | ||
let text = hint.displayParts | ||
.map(d => d.text) | ||
.join('') | ||
.replace(/\r?\n\s*/g, ' '); | ||
if (text.length > 120) { | ||
text = text.slice(0, 119) + '...'; | ||
} | ||
return { | ||
kind: sandbox.monaco.languages.InlayHintKind.Type, | ||
position: new sandbox.monaco.Position( | ||
endPos.lineNumber, | ||
endPos.column + 1, | ||
), | ||
label: text, | ||
paddingLeft: true, | ||
}; | ||
} | ||
}, | ||
}; | ||
} |
8 changes: 8 additions & 0 deletionspackages/website/src/components/editor/useSandboxServices.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.