mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 16:04:21 +00:00
feat(editor): structured Julia syntax highlighting (#4161)
Julia used CodeMirror's legacy StreamLanguage mode, which has no tags for function calls, type names or macros. It now uses Pluto's MIT-licensed Lezer grammar (@plutojl/lang-julia), with call targets tagged as function names, and Julia joins the language-scoped Color Scheme settings with a preview sample. Co-authored-by: Royshare <30562365+Royshare@users.noreply.github.com>
This commit is contained in:
@@ -194,6 +194,36 @@ declarations, key and animation binding names (`(keydown.enter)`, `[@fade]`,
|
||||
`(@fade.done)`), runs of text that entities split (`a && b`) parsed as one, and one
|
||||
token for `{` and `}`.
|
||||
|
||||
## Julia CodeMirror language support
|
||||
|
||||
[`@plutojl/lang-julia`](https://github.com/JuliaPluto/lang-julia) 1.2.0 and its
|
||||
[`@plutojl/lezer-julia`](https://github.com/JuliaPluto/lezer-julia) parser dependency
|
||||
provide the Julia CodeMirror 6 language mode. Both packages are MIT licensed;
|
||||
the full license text is below. The mode wraps a Lezer grammar and exposes its
|
||||
syntax tags through `julia()`.
|
||||
|
||||
### MIT License (@plutojl/lang-julia and @plutojl/lezer-julia)
|
||||
|
||||
Copyright (c) 2021 Andrey Popp
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
## nvim-treesitter queries
|
||||
|
||||
`crates/ide-syntax/queries/<grammar>/locals.scm` are derived from the `locals.scm`
|
||||
|
||||
@@ -67,6 +67,7 @@
|
||||
"@lezer/common": "^1.5.3",
|
||||
"@lezer/highlight": "^1.2.5",
|
||||
"@lezer/lr": "^1.4.11",
|
||||
"@plutojl/lang-julia": "^1.2.0",
|
||||
"@replit/codemirror-vim": "6.4.0",
|
||||
"@tanstack/react-virtual": "^3.14.21",
|
||||
"@tauri-apps/api": "^2",
|
||||
|
||||
Generated
+22
@@ -80,6 +80,9 @@ importers:
|
||||
'@lezer/lr':
|
||||
specifier: ^1.4.11
|
||||
version: 1.4.11
|
||||
'@plutojl/lang-julia':
|
||||
specifier: ^1.2.0
|
||||
version: 1.2.0
|
||||
'@replit/codemirror-vim':
|
||||
specifier: 6.4.0
|
||||
version: 6.4.0(@codemirror/commands@6.11.1)(@codemirror/language@6.13.1)(@codemirror/search@6.7.2)(@codemirror/state@6.7.6)(@codemirror/view@6.43.14)
|
||||
@@ -1000,6 +1003,12 @@ packages:
|
||||
engines: {node: '>=20'}
|
||||
hasBin: true
|
||||
|
||||
'@plutojl/lang-julia@1.2.0':
|
||||
resolution: {integrity: sha512-xItjyoS4HFj4BrvehQHCa47fh7I3kvvWkNNK6TIEva1fxQ/aYP8FL+4M7axntH/p/ZGh2vKMWia/GZ+gcgdJ4w==}
|
||||
|
||||
'@plutojl/lezer-julia@1.2.0':
|
||||
resolution: {integrity: sha512-7r14LZlBf7GGCDfS1ywbpiyJBSMlqQQJoa87s/WF4BnqaHklZsXPc0Ib/3tIp24BSTZryJvaSelGjO2VF3geRA==}
|
||||
|
||||
'@replit/codemirror-vim-core@0.1.0':
|
||||
resolution: {integrity: sha512-1i6EBKpcNfDKvTmTh6N6g9lL6udD5t+uFNh4JCqozRnVlvUGOps7h/QzS2ne4zcvPUjvApKpmcP7Grc3fNbZiQ==}
|
||||
|
||||
@@ -3081,6 +3090,19 @@ snapshots:
|
||||
dependencies:
|
||||
playwright: 1.64.1
|
||||
|
||||
'@plutojl/lang-julia@1.2.0':
|
||||
dependencies:
|
||||
'@codemirror/autocomplete': 6.20.3
|
||||
'@codemirror/language': 6.13.1
|
||||
'@lezer/common': 1.5.3
|
||||
'@plutojl/lezer-julia': 1.2.0
|
||||
|
||||
'@plutojl/lezer-julia@1.2.0':
|
||||
dependencies:
|
||||
'@lezer/common': 1.5.3
|
||||
'@lezer/highlight': 1.2.5
|
||||
'@lezer/lr': 1.4.11
|
||||
|
||||
'@replit/codemirror-vim-core@0.1.0': {}
|
||||
|
||||
'@replit/codemirror-vim@6.4.0(@codemirror/commands@6.11.1)(@codemirror/language@6.13.1)(@codemirror/search@6.7.2)(@codemirror/state@6.7.6)(@codemirror/view@6.43.14)':
|
||||
|
||||
@@ -7,6 +7,58 @@ import { logWarning } from "./highlight";
|
||||
import { fencedCodeMode, findLanguageDescription, loadSupport } from "./languages";
|
||||
|
||||
describe("language modes", () => {
|
||||
it("highlights Julia declarations, calls, types, macros and literals without semantic tokens", async () => {
|
||||
const description = findLanguageDescription("Julia", "/tmp/palette.jl");
|
||||
expect(description?.name).toBe("Julia");
|
||||
const support = await loadSupport(description!);
|
||||
const source = [
|
||||
"struct Point end",
|
||||
"function distance(p::Point)",
|
||||
' label = "point"',
|
||||
" norm(p)",
|
||||
" return 3, 3.5",
|
||||
"end",
|
||||
"@show distance(Point())",
|
||||
"# comment",
|
||||
].join("\n");
|
||||
const found: Array<[string, string]> = [];
|
||||
highlightTree(
|
||||
support!.language.parser.parse(source),
|
||||
tagHighlighter([
|
||||
{ tag: t.keyword, class: "keyword" },
|
||||
{ tag: t.function(t.definition(t.variableName)), class: "function-definition" },
|
||||
{ tag: t.function(t.variableName), class: "function-call" },
|
||||
{ tag: t.definition(t.typeName), class: "type-definition" },
|
||||
{ tag: t.typeName, class: "type-reference" },
|
||||
{ tag: t.macroName, class: "macro" },
|
||||
{ tag: t.integer, class: "integer" },
|
||||
{ tag: t.float, class: "float" },
|
||||
{ tag: t.string, class: "string" },
|
||||
{ tag: t.lineComment, class: "comment" },
|
||||
]),
|
||||
(from, to, style) => found.push([source.slice(from, to), style]),
|
||||
);
|
||||
|
||||
expect(found).toEqual([
|
||||
["struct", "keyword"],
|
||||
["Point", "type-definition"],
|
||||
["end", "keyword"],
|
||||
["function", "keyword"],
|
||||
["distance", "function-definition"],
|
||||
["Point", "type-reference"],
|
||||
['"point"', "string"],
|
||||
["norm", "function-call"],
|
||||
["return", "keyword"],
|
||||
["3", "integer"],
|
||||
["3.5", "float"],
|
||||
["end", "keyword"],
|
||||
["@show", "macro"],
|
||||
["distance", "function-call"],
|
||||
["Point", "function-call"],
|
||||
["# comment", "comment"],
|
||||
]);
|
||||
});
|
||||
|
||||
it("distinguishes PHP classes, method declarations and fields for imported schemes", async () => {
|
||||
const support = await loadSupport(findLanguageDescription("PHP", "/tmp/palette.php")!);
|
||||
const source = "<?php class Palette extends BasePalette { var $name; function example() {} }";
|
||||
|
||||
@@ -98,6 +98,22 @@ const supports = new Map<string, Promise<LanguageSupport | null>>();
|
||||
|
||||
/** Frontend modes that extend or replace the bundled parsers. */
|
||||
const OWN_MODES = [
|
||||
LanguageDescription.of({
|
||||
name: "Julia",
|
||||
extensions: ["jl"],
|
||||
load: async () => {
|
||||
const { julia } = await import("@plutojl/lang-julia");
|
||||
const support = julia();
|
||||
// The package's call rule matches VariableName nodes, but its grammar
|
||||
// represents ordinary call targets as Identifier nodes. Cover those too
|
||||
// so a call is not colored like an uncalled variable.
|
||||
const language = support.language as LRLanguage;
|
||||
return new LanguageSupport(
|
||||
language.configure({ props: [styleTags({ "CallExpression/Identifier": t.function(t.variableName) })] }),
|
||||
support.support,
|
||||
);
|
||||
},
|
||||
}),
|
||||
LanguageDescription.of({
|
||||
name: "CMake",
|
||||
extensions: ["cmake", "cmake.in"],
|
||||
|
||||
@@ -179,6 +179,19 @@ describe("python scheme attributes", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("Julia scheme attributes", () => {
|
||||
it("previews Julia roles and selects their independently scoped attributes", () => {
|
||||
schemePage("editor.colorScheme.julia");
|
||||
const preview = screen.getByLabelText("Color scheme preview");
|
||||
act(() => fireEvent.click(preview.querySelector('span[style*="--syn-function-call"]')!));
|
||||
expect(screen.getByRole("treeitem", { name: "Function call", selected: true })).toBeInTheDocument();
|
||||
act(() => fireEvent.click(preview.querySelector('span[style*="--syn-type"]')!));
|
||||
expect(screen.getByRole("treeitem", { name: "Class name", selected: true })).toBeInTheDocument();
|
||||
act(() => fireEvent.click(within(preview).getByText("@show")));
|
||||
expect(screen.getByRole("treeitem", { name: "Metadata", selected: true })).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
describe("doc comment scheme attributes", () => {
|
||||
it.each([
|
||||
["en", "Doc comment", "Tag", "Tag value", "Markup"],
|
||||
|
||||
@@ -767,6 +767,40 @@ const SAMPLES: Record<string, CodeSample> = {
|
||||
[["}", "java.operator"]],
|
||||
],
|
||||
},
|
||||
Julia: {
|
||||
code: [
|
||||
[["# Julia", "julia.lineComment"]],
|
||||
[
|
||||
["struct ", "julia.keyword"],
|
||||
["Point", "julia.className"],
|
||||
["{", "julia.operator"],
|
||||
],
|
||||
[[" x::Float64", "julia.identifier"]],
|
||||
[["end", "julia.keyword"]],
|
||||
[],
|
||||
[
|
||||
["function ", "julia.keyword"],
|
||||
["distance", "julia.functionDeclaration"],
|
||||
["(p::", "julia.operator"],
|
||||
["Point", "julia.className"],
|
||||
[")", "julia.operator"],
|
||||
],
|
||||
[[" norm((p.x, p.y))", "text"]],
|
||||
[["end", "julia.keyword"]],
|
||||
[],
|
||||
[
|
||||
["@show ", "julia.metadata"],
|
||||
["distance", "julia.functionCall"],
|
||||
["(", "julia.operator"],
|
||||
["Point", "julia.functionCall"],
|
||||
["(", "julia.operator"],
|
||||
["1.0", "julia.number"],
|
||||
[", ", "julia.operator"],
|
||||
["2.0", "julia.number"],
|
||||
["))", "julia.operator"],
|
||||
],
|
||||
],
|
||||
},
|
||||
Go: {
|
||||
code: [
|
||||
[["package ", "go.keyword"], ["palette"]],
|
||||
|
||||
@@ -73,6 +73,19 @@ describe("color schemes", () => {
|
||||
expect(css).toContain("--syn-keyword: #224466 !important");
|
||||
});
|
||||
|
||||
it("offers a Julia scheme page and scopes Julia overrides to Julia editors", () => {
|
||||
expect(ATTRIBUTES.find((a) => a.id === "julia.functionCall")?.path).toEqual(["Julia"]);
|
||||
expect(ATTRIBUTES.find((a) => a.id === "julia.className")?.path).toEqual(["Julia"]);
|
||||
const css = schemeCss("dark", {
|
||||
dark: { "julia.functionCall": { foreground: "#224466" }, "julia.className": { italic: true } },
|
||||
});
|
||||
expect(css).toContain('.cm-editor[data-language="Julia"]');
|
||||
expect(css).toContain('.cm-editor [data-language="Julia"]');
|
||||
expect(css).toContain("--syn-function-call: #224466 !important");
|
||||
expect(css).toContain("--syn-type-italic: italic !important");
|
||||
expect(css.indexOf('.cm-editor[data-language="Julia"]')).toBeGreaterThan(css.indexOf(":root"));
|
||||
});
|
||||
|
||||
it("groups Python attributes as PyCharm's color settings page does", () => {
|
||||
const python = (name: string) => ATTRIBUTES.find((a) => a.id === `py${name}`)!;
|
||||
expect(python("Number").name).toBe("Number");
|
||||
|
||||
@@ -377,7 +377,14 @@ const BASE_ATTRIBUTES: readonly AttributeDef[] = [
|
||||
syn("regexp", "Regular expression", ["RegExp"], "regexp"),
|
||||
];
|
||||
|
||||
const SCHEME_LANGUAGES = { csharp: "C#", go: "Go", java: "Java", kotlin: "Kotlin", php: "PHP" } as const;
|
||||
const SCHEME_LANGUAGES = {
|
||||
csharp: "C#",
|
||||
go: "Go",
|
||||
java: "Java",
|
||||
julia: "Julia",
|
||||
kotlin: "Kotlin",
|
||||
php: "PHP",
|
||||
} as const;
|
||||
/** Languages that scope their overrides into a per-language editor block; Python ships its own attributes. */
|
||||
const SCHEME_LANGUAGE_SCOPES = { ...SCHEME_LANGUAGES, python: "Python" } as const;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user