Compare commits

...
Author SHA1 Message Date
l0ng-ai daa444fbf5 fix(editor): highlight shell commands, function names and local from the grammar
The legacy shell stream mode works from fixed word lists: local, declare and
return were plain text, and so were function definitions and any command
outside its ~70 builtins. The bundled tree-sitter-bash grammar now draws its
highlights over the stream mode, as Kotlin's does: declaration keywords,
function definition names, command names and variables. Command names get
their own colour (--syn-shell-command, Color Scheme › Shell Script ›
Command), blue in light and orange in dark schemes, falling back to the
function-call colour in colour themes.
2026-11-15 05:02:55 +08:00
10 changed files with 115 additions and 8 deletions

No files matched your search

@@ -0,0 +1,56 @@
; Shell scripts: drawn over the stream mode, which knows only fixed word lists.
; The last matching pattern wins.
[
(string)
(raw_string)
(ansi_c_string)
(translated_string)
(heredoc_body)
(heredoc_start)
] @string
[
(command_substitution)
(process_substitution)
(expansion)
(simple_expansion)
] @embedded
(comment) @comment
[
"case"
"do"
"done"
"elif"
"else"
"esac"
"fi"
"for"
"function"
"if"
"in"
"select"
"then"
"until"
"while"
"declare"
"export"
"local"
"readonly"
"typeset"
"unset"
"unsetenv"
] @keyword
(command_name) @function.command
(function_definition name: (word) @function.definition)
(variable_name) @variable
(special_variable_name) @variable
[
(number)
(file_descriptor)
] @number
+1 -1
View File
@@ -314,7 +314,7 @@ pub(crate) static SPECS: &[Spec] = &[
name: "bash",
language: tree_sitter_bash::LANGUAGE,
outline: outline!("bash"),
highlights: None,
highlights: Some(Highlights { query: highlights!("bash"), first_wins: false }),
fold_nodes: &["do_group", "if_statement", "case_statement", "heredoc_body"],
fold_bodies: NONE,
imports: NONE,
+29 -1
View File
@@ -43,11 +43,13 @@ pub enum SyntaxHighlightKind {
Operator,
Punctuation,
Parameter,
/// A shell command name (`pgrep`, a script's own function).
Command,
}
impl SyntaxHighlightKind {
/// Every kind, in `as u32` order: the legend for [`SyntaxHighlights::data`].
pub const ALL: [Self; 25] = [
pub const ALL: [Self; 26] = [
Self::Keyword,
Self::String,
Self::StringSpecial,
@@ -73,6 +75,7 @@ impl SyntaxHighlightKind {
Self::Operator,
Self::Punctuation,
Self::Parameter,
Self::Command,
];
/// The kebab-case name plugins use (`doc-comment`, `function-call`, …).
@@ -153,6 +156,7 @@ const CAPTURES: &[(&str, Paint)] = {
("function.builtin", Kind(K::Builtin)),
("function.macro", Kind(K::Macro)),
("function.definition", Kind(K::Function)),
("function.command", Kind(K::Command)),
("function", Kind(K::FunctionCall)),
("method", Kind(K::FunctionCall)),
("constructor", Kind(K::Type)),
@@ -408,6 +412,30 @@ mod tests {
}
}
#[test]
fn shell_scripts_highlight_declarations_commands_and_function_names() {
use SyntaxHighlightKind as K;
let src = "this::check_process() {\n local process\n process=\"${1:?}\"\n if [ -x \"$(command -v pgrep)\" ]; then\n if pgrep -f -q \"${process}\"; then\n functions::exit 1 \"'${process}' is running\"\n fi\n fi\n}\n# done\n";
let got = spans("shellscript", src);
for (text, expected) in [
("this::check_process", K::Function),
("local", K::Keyword),
("if", K::Keyword),
("command", K::Command),
("pgrep", K::Command),
("functions::exit", K::Command),
("process", K::Variable),
("# done", K::Comment),
] {
assert!(
got.iter().any(|(word, kind)| word == text && *kind == expected),
"missing {text}: {expected:?} in {got:?}"
);
}
// An expansion inside a string is not string-coloured.
assert!(!got.iter().any(|(word, kind)| word.contains("${process}") && *kind == K::String), "{got:?}");
}
#[test]
fn non_kotlin_parameters_keep_variable_highlighting() {
for (lang, src) in [
+3 -1
View File
@@ -10197,7 +10197,9 @@ export type SyntaxErrors = {
};
/** Highlight classes sent to the UI, which maps each onto a Lezer tag. */
export type SyntaxHighlightKind = "keyword" | "string" | "stringSpecial" | "escape" | "regexp" | "atom" | "number" | "constant" | "comment" | "docComment" | "function" | "functionCall" | "builtin" | "type" | "namespace" | "variable" | "selfVariable" | "property" | "attribute" | "macro" | "label" | "tag" | "operator" | "punctuation" | "parameter";
export type SyntaxHighlightKind = "keyword" | "string" | "stringSpecial" | "escape" | "regexp" | "atom" | "number" | "constant" | "comment" | "docComment" | "function" | "functionCall" | "builtin" | "type" | "namespace" | "variable" | "selfVariable" | "property" | "attribute" | "macro" | "label" | "tag" | "operator" | "punctuation" | "parameter" |
/** A shell command name (`pgrep`, a script's own function). */
"command";
/** Highlighted ranges of a whole document, packed like LSP semantic tokens. */
export type SyntaxHighlights = {
+3
View File
@@ -9,6 +9,8 @@ import { Tag, tags as t } from "@lezer/highlight";
/** Log warnings: the UI warning colour, since no syntax token reads as one in every scheme. */
export const logWarning = Tag.define("logWarning");
export const parameter = Tag.define(t.variableName);
/** A shell command name: its own colour in JetBrains schemes, else a function call. */
export const shellCommand = Tag.define("shellCommand");
/** Inside doc comments, as IntelliJ colours them: `@param` / `{@link`, a `@param` name, HTML markup. */
export const docCommentTag = Tag.define(t.docComment);
export const docCommentTagValue = Tag.define(t.docComment);
@@ -60,6 +62,7 @@ export const openideaHighlightStyle = HighlightStyle.define([
token([t.typeName, t.className, t.namespace, t.definition(t.typeName)], "type"),
token([t.standard(t.typeName), t.standard(t.variableName), t.standard(t.function(t.variableName))], "builtin"),
token([t.function(t.variableName), t.function(t.propertyName)], "function-call"),
token(shellCommand, "shell-command", { color: "function-call" }),
token(
[
t.function(t.definition(t.variableName)),
+9 -1
View File
@@ -3,7 +3,7 @@ import { EditorState } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import { highlightingFor, syntaxHighlighting } from "@codemirror/language";
import { tags as t } from "@lezer/highlight";
import { openideaHighlightStyle, parameter } from "./highlight";
import { openideaHighlightStyle, parameter, shellCommand } from "./highlight";
import { findLanguageDescription, loadSupport } from "./languages";
import { setSyntaxHighlights, syntaxHighlightField } from "./syntaxHighlight";
@@ -46,6 +46,14 @@ describe("syntaxHighlightField", () => {
expect(marks(state).map((m) => m.text)).toEqual(["def", "hi", ":ok"]);
});
it("gives shell command names their own class, apart from function calls", () => {
const state = create().update({ effects: setSyntaxHighlights.of({ kinds: ["command"], data: [0, 3, 0] }) }).state;
const command = highlightingFor(state, [shellCommand]);
expect(command).toBeTruthy();
expect(command).not.toBe(highlightingFor(state, [t.function(t.variableName)]));
expect(marks(state)).toEqual([{ text: "def", cls: command }]);
});
it("lets Kotlin declaration roles override the stream lexer's generic definition style", async () => {
const support = await loadSupport(findLanguageDescription("Kotlin", "demo.kt")!);
const view = new EditorView({
+3 -2
View File
@@ -1,6 +1,6 @@
/**
* Tree-sitter highlighting (`doc_highlights`) for languages without a
* CodeMirror mode (Elixir, ...) or a limited stream mode (Kotlin): backend kinds → Lezer tags, so the colours
* CodeMirror mode (Elixir, ...) or a limited stream mode (Kotlin, Shell): backend kinds → Lezer tags, so the colours
* come from the same highlight style and Color Scheme settings as Lezer
* highlighting. Refreshed with folding; in between, marks are mapped through
* the changes.
@@ -9,7 +9,7 @@ import { Prec, StateEffect, StateField, type EditorState } from "@codemirror/sta
import { Decoration, EditorView, type DecorationSet } from "@codemirror/view";
import { highlightingFor } from "@codemirror/language";
import { tags as t, type Tag } from "@lezer/highlight";
import { parameter } from "./highlight";
import { parameter, shellCommand } from "./highlight";
import type { SyntaxHighlightKind, SyntaxHighlights } from "@/platform/ipc";
const TAGS: Record<SyntaxHighlightKind, Tag> = {
@@ -38,6 +38,7 @@ const TAGS: Record<SyntaxHighlightKind, Tag> = {
tag: t.tagName,
operator: t.operator,
punctuation: t.punctuation,
command: shellCommand,
};
export const setSyntaxHighlights = StateEffect.define<SyntaxHighlights>();
+6 -2
View File
@@ -98,6 +98,11 @@ export const useDocMeta = create<Record<string, DocMeta>>(() => ({}));
export const useOutline = create<Record<string, OutlineItem[]>>(() => ({}));
const REFRESH_DELAY = 450;
/**
* Languages whose stream mode works from word lists, so the grammar's highlights are drawn
* over it: Kotlin declaration roles, Shell commands, function names and `local`.
*/
const GRAMMAR_SUPPLEMENTED = new Set(["kotlin", "shellscript"]);
function bannerFor(info: DocumentInfo): Banner {
// Virtual documents (library classes) get their own notice from the editor area.
if (info.uri) return null;
@@ -574,8 +579,7 @@ export class DocModel {
const [folds, outline, highlights] = await Promise.allSettled([
backend.folding(this.doc),
backend.outline(this.doc),
// Kotlin's stream mode lacks declaration categories; the grammar supplements it.
!this.desc || this.meta.language === "kotlin" ? backend.highlights(this.doc) : null,
!this.desc || GRAMMAR_SUPPLEMENTED.has(this.meta.language) ? backend.highlights(this.doc) : null,
]);
if (this.closed) return;
if (counter !== this.changeCounter) {
+1
View File
@@ -364,6 +364,7 @@ const BASE_ATTRIBUTES: readonly AttributeDef[] = [
italicOnly: true,
defaultItalic: false,
},
syn("shellCommand", "Command", ["Shell Script"], "shell-command"),
syn("mdHeading", "Heading", ["Markdown"], "markup-heading", { defaultBold: true }),
syn("mdCode", "Code span", ["Markdown"], "markup-code"),
syn("mdLink", "Link", ["Markdown"], "link", { defaultEffect: "underline" }),
+4
View File
@@ -114,6 +114,7 @@ html {
--syn-type: #bcbec4;
--syn-function: #56a8f5;
--syn-function-call: #bcbec4;
--syn-shell-command: #c57633;
--syn-comment: #7a7e85;
--syn-comment-style: normal;
--syn-doc-comment: #5f826b;
@@ -558,6 +559,7 @@ html {
--syn-type: #000000;
--syn-function: #00627a;
--syn-function-call: #080808;
--syn-shell-command: #0073bf;
--syn-comment: #8c8c8c;
--syn-comment-style: italic;
--syn-doc-comment: #8c8c8c;
@@ -856,6 +858,7 @@ html {
--syn-type: #e3e4e8;
--syn-function: #7cc0ff;
--syn-function-call: #e3e4e8;
--syn-shell-command: #c57633;
--syn-comment: #a5a9b0;
--syn-doc-comment: #8fb89c;
--syn-doc-comment-tag: #8fcca3;
@@ -1244,6 +1247,7 @@ html {
--syn-type: var(--palette-type);
--syn-function: var(--palette-blue);
--syn-function-call: var(--palette-blue);
--syn-shell-command: var(--syn-function-call);
--syn-comment: var(--palette-comment);
--syn-doc-comment: var(--palette-comment);
--syn-doc-comment-tag: var(--palette-keyword);