Skip to content

The IDE you know, rebuilt in Rust. IntelliJ IDEA–style keymap, tool windows and search-everywhere in a fast native app: editor, code insight, debugger, Git, terminal, database and HTTP clients for 60+ languages.

4.9 KB
Raw Blame
/**
 * Syntax colours: lezer highlight tags → `--syn-*` tokens (PyCharm Islands
 * schemes). Each token also reads `--syn-<token>-bold / -italic / -bg /
 * -effect`, set by Settings › Editor › Color Scheme overrides.
 */
import { HighlightStyle, type TagStyle } from "@codemirror/language";
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);
/** 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);
export const docCommentMarkup = Tag.define(t.docComment);

function token(
  tag: Tag | Tag[],
  name: string,
  defaults: { italic?: string; bold?: string; effect?: string; color?: string } = {},
): TagStyle {
  const variable = (suffix: string, fallback: string) =>
    `var(--syn-${name}${suffix}, ${defaults.color ? `var(--syn-${defaults.color}${suffix}, ${fallback})` : fallback})`;
  return {
    tag,
    color: defaults.color ? variable("", "inherit") : `var(--syn-${name})`,
    fontStyle: variable("-italic", defaults.italic ?? "inherit"),
    fontWeight: variable("-bold", defaults.bold ?? "inherit"),
    backgroundColor: variable("-bg", "transparent"),
    textDecoration: variable("-effect", defaults.effect ?? "none"),
    // WebKit takes a decoration with a style or colour only through the prefixed property.
    WebkitTextDecoration: variable("-effect", defaults.effect ?? "none"),
  };
}

export const openideaHighlightStyle = HighlightStyle.define([
  // Markdown tags whole links / quotes; listed first so marks, URL and title inside keep their own colours.
  token(t.link, "link", { effect: "underline" }),
  token(t.quote, "string"),
  token(
    [
      t.keyword,
      t.controlKeyword,
      t.moduleKeyword,
      t.operatorKeyword,
      t.definitionKeyword,
      t.modifier,
      t.null,
      t.bool,
      t.unit,
    ],
    "keyword",
  ),
  token(t.self, "self"),
  token([t.string, t.special(t.string), t.character, t.attributeValue], "string"),
  token(t.docString, "docstring", { italic: "italic" }),
  token(t.escape, "escape"),
  token(t.regexp, "regexp"),
  token([t.number, t.integer, t.float], "number"),
  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(
    [
      t.function(t.definition(t.variableName)),
      t.definition(t.function(t.variableName)),
      t.function(t.definition(t.propertyName)),
    ],
    "function",
  ),
  token([t.comment, t.lineComment, t.blockComment], "comment", { italic: "var(--syn-comment-style)" }),
  token(t.docComment, "doc-comment", { italic: "italic" }),
  token(docCommentTag, "doc-comment-tag", { italic: "normal", bold: "var(--fw-code, 400)" }),
  token(docCommentTagValue, "doc-comment-tag-value", { italic: "normal", bold: "var(--fw-code, 400)" }),
  token(docCommentMarkup, "doc-markup", { italic: "normal", bold: "var(--fw-code, 400)" }),
  token([t.meta, t.annotation, t.macroName], "decorator"),
  // `<!doctype>` is a sub-tag of `meta`; PyCharm leaves the XML prologue as plain text.
  { tag: t.documentMeta, color: "var(--fg-editor)" },
  token([t.constant(t.variableName), t.atom, t.special(t.variableName)], "constant", { italic: "italic" }),
  token(t.labelName, "constant"),
  token([t.variableName, t.definition(t.variableName), t.local(t.variableName)], "variable"),
  token([t.propertyName, t.definition(t.propertyName)], "property"),
  token(parameter, "param"),
  token(t.attributeName, "attribute"),
  token(t.special(t.brace), "decorator"),
  token([t.tagName, t.angleBracket], "tag"),
  token([t.punctuation, t.bracket, t.separator, t.operator, t.derefOperator], "punct"),
  token(t.bracket, "bracket", { color: "punct" }),
  token(t.separator, "separator", { color: "punct" }),
  token(t.invalid, "invalid"),
  // Markup (Markdown, docs)
  token(t.heading, "markup-heading", { bold: "var(--fw-code-bold, 700)" }),
  token(t.processingInstruction, "processing-instruction", { color: "keyword" }),
  { tag: t.emphasis, fontStyle: "italic" },
  { tag: t.strong, fontWeight: "var(--fw-code-bold, 700)" },
  { tag: t.strikethrough, textDecoration: "line-through" },
  // Markdown, as IntelliJ: link destination like a static method, rules like markers.
  token(t.url, "function", { italic: "italic" }),
  token(t.monospace, "markup-code"),
  token(t.contentSeparator, "keyword"),
  { tag: logWarning, color: "var(--warning)" },
  { tag: t.inserted, color: "var(--vcs-added)" },
  { tag: t.deleted, color: "var(--danger)" },
  { tag: t.changed, color: "var(--vcs-modified)" },
]);