дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-06 10:07:11 +02:00
Code shown outside an editor tab (diffs, merge editor, revision browser, previews of closed files) picked its language on the frontend from the file extension, while the editor uses the backend's detection, which also checks overrides, File Types patterns and the first line. A single resolver, languageDescriptionFor, now uses the open editor's mode when the file is open and otherwise asks the backend (doc_language, replacing doc_ui_mode) to detect it from the shown text's first line, or the open buffer / file on disk, falling back to the extension only when that fails. Closes #3799 Co-authored-by: bertyhell <[email protected]>
39 рядки
1.5 KiB
TypeScript
39 рядки
1.5 KiB
TypeScript
/**
|
|
* Code previews outside editor tabs (Find in Files, bookmarks): the
|
|
* editor's font, colours, line numbers and syntax highlighting, without
|
|
* folding or contributions. Editing is opt-in.
|
|
*/
|
|
import { EditorState, type Extension } from "@codemirror/state";
|
|
import { EditorView, highlightSpecialChars, lineNumbers } from "@codemirror/view";
|
|
import { syntaxHighlighting } from "@codemirror/language";
|
|
import type { EditorSettings } from "@/platform/ipc";
|
|
import type { DocBackend } from "../backend";
|
|
import { openideaHighlightStyle } from "./highlight";
|
|
import { openideaTheme } from "./theme";
|
|
import { languageDescriptionFor, loadSupport, loadedSupport } from "./languages";
|
|
import { fontExtension } from "./setup";
|
|
import { tx } from "@/platform/i18n";
|
|
import { selectableText } from "@/ui";
|
|
|
|
export async function previewExtensions(
|
|
path: string,
|
|
settings: EditorSettings | undefined,
|
|
backend?: DocBackend,
|
|
editable = false,
|
|
): Promise<Extension> {
|
|
const desc = await languageDescriptionFor(path, undefined, backend);
|
|
const support = desc ? (loadedSupport(desc) ?? (await loadSupport(desc))) : null;
|
|
return [
|
|
fontExtension(settings),
|
|
openideaTheme,
|
|
lineNumbers(),
|
|
highlightSpecialChars(),
|
|
syntaxHighlighting(openideaHighlightStyle),
|
|
EditorState.readOnly.of(!editable),
|
|
EditorView.editable.of(editable),
|
|
// Read-only previews still support selection, Copy and keyboard focus.
|
|
EditorView.contentAttributes.of({ "aria-label": tx`Preview of ${path}`, class: selectableText, tabindex: "0" }),
|
|
support ?? [],
|
|
];
|
|
}
|