Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
bradzacher merged 7 commits intotypescript-eslint:mainfromauvred:chore/8055
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file
Open in desktop
Original file line numberDiff line numberDiff 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

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,
};
}
},
};
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,6 +10,7 @@ import { createFileSystem } from '../linter/bridge';
import { type CreateLinter, createLinter } from '../linter/createLinter';
import type { PlaygroundSystem } from '../linter/types';
import type { RuleDetails } from '../types';
import { createTwoslashInlayProvider } from './createProvideTwoslashInlay';
import { editorEmbedId } from './EditorEmbed';
import { sandboxSingleton } from './loadSandbox';
import type { CommonEditorProps } from './types';
Expand DownExpand Up@@ -71,6 +72,13 @@ export const useSandboxServices = (
colorMode === 'dark' ? 'vs-dark' : 'vs-light',
);

// registerInlayHintsProvider was added in TS 4.4 and isn't in TS <= 4.3.
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
sandboxInstance.monaco.languages.registerInlayHintsProvider?.(
sandboxInstance.language,
createTwoslashInlayProvider(sandboxInstance),
);

const system = createFileSystem(props, sandboxInstance.tsvfs);

const worker = await sandboxInstance.getWorkerProcess();
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp