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.
TypeScript code-editorcross-platformdapdatabase-clientdebuggerdesktop-appdeveloper-toolsgithttp-clientideintellijlsprusttauriterminal
feat(editor): reStructuredText highlighting and section outline (#2376)
LU XIANGRUI358808833ced
diff --git a/crates/ide-language/src/registry.rs b/crates/ide-language/src/registry.rsindex 99b35d7e3..2f0b81217 100644--- a/crates/ide-language/src/registry.rs+++ b/crates/ide-language/src/registry.rs@@ -70,6 +70,7 @@ const BUILTIN: &[(&str, &str)] = &[("proto", include_str!("../../../languages/proto.toml")),("python", include_str!("../../../languages/python.toml")),("r", include_str!("../../../languages/r.toml")),+ ("restructuredtext", include_str!("../../../languages/restructuredtext.toml")),("ruby", include_str!("../../../languages/ruby.toml")),("rust", include_str!("../../../languages/rust.toml")),("sass", include_str!("../../../languages/sass.toml")),@@ -562,6 +563,7 @@ mod tests {"R","RPM Changes","RPM Spec",+ "reStructuredText","Ruby","SAS","Scala",@@ -628,6 +630,12 @@ mod tests {let asciidoc = reg.get(&LanguageId::new("asciidoc")).unwrap();assert_eq!(asciidoc.ui_mode.as_deref(), Some("AsciiDoc"));assert_eq!(asciidoc.line_comment.as_deref(), Some("//"));+ for path in ["guide.rst", "README.RST"] {+ assert_eq!(detect(®, path), "restructuredtext");+ }+ let rst = reg.get(&LanguageId::new("restructuredtext")).unwrap();+ assert_eq!(rst.ui_mode.as_deref(), Some("reStructuredText"));+ assert_eq!(rst.line_comment.as_deref(), Some(".."));assert_eq!(reg.detect(Path::new("script"), Some("#!/usr/bin/env python3")).as_str(), "python");assert_eq!(detect(®, "README"), "plaintext");assert_eq!(detect(®, "unknown.zzz"), "plaintext");diff --git a/crates/ide-language/src/syntax/cache.rs b/crates/ide-language/src/syntax/cache.rsindex 5dc99a706..6baeb034b 100644--- a/crates/ide-language/src/syntax/cache.rs+++ b/crates/ide-language/src/syntax/cache.rs@@ -206,6 +206,7 @@ impl SyntaxCache {// their trees join the cache..or_else(|| self.syntax.web_outline(lang, text)).or_else(|| super::external::outline(lang, text))+ .or_else(|| self.syntax.builtin_outline(lang, text))},)}diff --git a/crates/ide-language/src/syntax/mod.rs b/crates/ide-language/src/syntax/mod.rsindex 90643f76a..a015c0c36 100644--- a/crates/ide-language/src/syntax/mod.rs+++ b/crates/ide-language/src/syntax/mod.rs@@ -9,6 +9,7 @@ mod highlight;pub(crate) mod lines;mod literals;mod outline;+mod rst;mod selection;mod sfc;mod web;@@ -177,9 +178,15 @@ impl Syntax {let query = g.outline_query()?;Some(outline::outline(g, query, tree, text))})+ .or_else(|| self.builtin_outline(lang, text)).unwrap_or_default()}+ /// Outline for languages without a grammar that have a built-in parser.+ fn builtin_outline(&self, lang: &LanguageId, text: &str) -> Option<Vec<OutlineItem>> {+ (lang.as_str() == "restructuredtext" && text.len() <= MAX_PARSE_BYTES).then(|| rst::outline(text))+ }+/// Outline of an HTML container document (html, vue, svelte, jsp): `None`/// for every other language, CSS containers included.pub(crate) fn web_outline(&self, lang: &LanguageId, text: &str) -> Option<Vec<OutlineItem>> {diff --git a/crates/ide-language/src/syntax/rst.rs b/crates/ide-language/src/syntax/rst.rsnew file mode 100644index 000000000..5671b7c91--- /dev/null+++ b/crates/ide-language/src/syntax/rst.rs@@ -0,0 +1,131 @@+//! reStructuredText section titles for the outline. No bundled grammar nests+//! sections (tree-sitter-rst emits them flat), so levels follow docutils: each+//! adornment style (character, with or without overline) takes the next level+//! the first time it appears.++use super::lines::LineIndex;+use ide_core::{OutlineItem, SymbolKind};++struct Line<'a> {+ start: usize,+ text: &'a str,+}++struct Heading {+ level: usize,+ start: usize,+ end: usize,+ name_start: usize,+ name_end: usize,+}++pub(crate) fn outline(text: &str) -> Vec<OutlineItem> {+ let lines: Vec<Line> = text+ .split_inclusive('\n')+ .scan(0, |at, raw| {+ let start = *at;+ *at += raw.len();+ Some(Line { start, text: raw.trim_end_matches(['\n', '\r']) })+ })+ .collect();+ let mut heads = headings(&lines);+ for k in 0..heads.len() {+ let end = heads[k + 1..].iter().find(|h| h.level <= heads[k].level).map_or(text.len(), |h| h.start);+ heads[k].end = heads[k].start + text[heads[k].start..end].trim_end().len();+ }+ let index = LineIndex::new(text);+ let mut i = 0;+ nest(&heads, &mut i, None, text, &index)+}++fn headings(lines: &[Line]) -> Vec<Heading> {+ let mut styles: Vec<(char, bool)> = Vec::new();+ let mut out = Vec::new();+ let mut push = |style: (char, bool), start: usize, title: &Line| {+ let level = styles.iter().position(|&s| s == style).unwrap_or_else(|| {+ styles.push(style);+ styles.len() - 1+ });+ let name = title.text.trim();+ let name_start = title.start + (title.text.len() - title.text.trim_start().len());+ out.push(Heading { level, start, end: 0, name_start, name_end: name_start + name.len() });+ };+ // A title starts a new block: it follows a blank line, the start, or another title.+ let mut at_block_start = true;+ let mut i = 0;+ while i < lines.len() {+ let line = &lines[i];+ if line.text.trim().is_empty() {+ at_block_start = true;+ i += 1;+ continue;+ }+ if at_block_start {+ if let Some(over) = adornment(line.text) {+ if let [title, under, ..] = &lines[i + 1..]+ && !title.text.trim().is_empty()+ && adornment(title.text).is_none()+ && adornment(under.text) == Some(over)+ && fits(title.text.trim_end(), over.1)+ {+ push((over.0, true), line.start, title);+ i += 3;+ continue;+ }+ } else if !line.text.starts_with([' ', '\t'])+ && !starts_body_element(line.text)+ && let Some(under) = lines.get(i + 1).and_then(|l| adornment(l.text))+ && fits(line.text.trim_end(), under.1)+ {+ push((under.0, false), line.start, line);+ i += 2;+ continue;+ }+ }+ // Indented lines (literal blocks after `::`, directive bodies, quotes)+ // never hold titles: they are skipped here because adornments and+ // underlined titles must start in column 0.+ at_block_start = false;+ i += 1;+ }+ out+}++/// `(character, length)` of a line made of one repeated ASCII punctuation character.+fn adornment(line: &str) -> Option<(char, usize)> {+ let line = line.trim_end();+ let c = line.chars().next().filter(char::is_ascii_punctuation)?;+ line.chars().all(|x| x == c).then_some((c, line.len()))+}++/// docutils accepts a short adornment (with a warning) unless it is shorter+/// than 4 characters, which reads as ordinary text instead.+// ponytail: counts chars, not display columns; wide CJK titles get a looser check.+fn fits(title: &str, adornment_len: usize) -> bool {+ adornment_len >= title.chars().count() || adornment_len >= 4+}++/// Lines docutils parses as another body element before trying a title:+/// explicit markup (`..`, `__`), bullets, field lists, doctests, line blocks.+fn starts_body_element(line: &str) -> bool {+ let marker = |m: &str| line == m || line.strip_prefix(m).is_some_and(|r| r.starts_with(' '));+ ["..", "__", "-", "*", "+", "•", ">>>", "|"].into_iter().any(marker)+ || (line.starts_with(':') && line[1..].find(':').is_some_and(|k| k > 0 && marker(&line[..k + 2])))+}++fn nest(heads: &[Heading], i: &mut usize, parent: Option<usize>, text: &str, index: &LineIndex) -> Vec<OutlineItem> {+ let mut items = Vec::new();+ while let Some(h) = heads.get(*i).filter(|h| parent.is_none_or(|p| h.level > p)) {+ *i += 1;+ items.push(OutlineItem {+ name: text[h.name_start..h.name_end].to_owned(),+ detail: None,+ kind: SymbolKind::Key,+ range: index.range(h.start, h.end),+ selection_range: index.range(h.name_start, h.name_end),+ children: nest(heads, i, Some(h.level), text, index),+ visibility: None,+ });+ }+ items+}diff --git a/crates/ide-language/src/syntax/tests.rs b/crates/ide-language/src/syntax/tests.rsindex be7f3d60a..23158c731 100644--- a/crates/ide-language/src/syntax/tests.rs+++ b/crates/ide-language/src/syntax/tests.rs@@ -1638,3 +1638,91 @@ fn script_outlines_compose_positions_across_multiple_blocks() {fn script_outline_attaches_to_case_insensitive_html_tags() {check_outline("html", "<SCRIPT>function boot() {}</SCRIPT>", "Field SCRIPT\n Function boot");}++#[test]+fn outline_restructuredtext_levels_follow_first_use_of_each_adornment() {+ let src = "\+=========+ Project+=========++Install+~~~~~~~++From source+-----------++Literal block::++ Not a title+ -----------++Intro+--++A paragraph+with a line+------------++.. comment+-----------++Usage+~~~~~++.. note::++ Indented+ --------++Short underline, still a title+------++Underline only+==============+";+ check_outline(+ "restructuredtext",+ src,+ "+ Key Project+ Key Install+ Key From source+ Key Usage+ Key Short underline, still a title+ Key Underline only+ ",+ );+ let items = syntax().outline(&lang("restructuredtext"), src);+ let project = &items[0];+ assert_eq!(project.range.start, Pos::new(0, 0));+ assert_eq!((project.selection_range.start, project.selection_range.end), (Pos::new(1, 1), Pos::new(1, 8)));+ let install = &project.children[0];+ assert_eq!((install.range.start, install.range.end), (Pos::new(4, 0), Pos::new(23, 11)));+ assert!(syntax().outline(&lang("restructuredtext"), "Title\n\n").is_empty());+}++#[test]+fn outline_restructuredtext_unicode_crlf_and_cached_edits() {+ use ide_core::DocId;+ use std::sync::Arc;++ let src = "=========\r\n 中文😀\r\n=========\r\n\r\n子章节\r\n------\r\n\r\nBody\r\n\r\nOther\r\n------\r\n";+ let id = DocId::next();+ let language = lang("restructuredtext");+ let cache = SyntaxCache::new(Arc::new(Syntax::new()));+ cache.open(id, &language, 0);+ let items = cache.outline(id, &language, 0, src);+ assert_eq!(items, syntax().outline(&language, src));+ assert_eq!(items[0].name, "中文😀");+ assert_eq!(items[0].selection_range, Range::new(Pos::new(1, 1), Pos::new(1, 5)));+ assert_eq!(items[0].children[0].name, "子章节");+ assert_eq!(items[0].children[0].selection_range, Range::new(Pos::new(4, 0), Pos::new(4, 3)));+ assert_eq!(items[0].children[0].range.end, Pos::new(7, 4));+ assert_eq!(items[0].children[1].name, "Other");+ let edited = src.replace("子章节", "新章节😀");+ let updated = cache.outline(id, &language, 1, &edited);+ assert_eq!(updated, syntax().outline(&language, &edited));+ assert_eq!(updated[0].children[0].selection_range.end, Pos::new(4, 5));+ assert_ne!(items, updated);+}diff --git a/languages/restructuredtext.toml b/languages/restructuredtext.tomlnew file mode 100644index 000000000..ca1b5b88b--- /dev/null+++ b/languages/restructuredtext.toml@@ -0,0 +1,6 @@+id = "restructuredtext"+name = "reStructuredText"+extensions = ["rst"]+line-comment = ".."+indent = { tab-size = 3, insert-spaces = true }+ui-mode = "reStructuredText"diff --git a/src/editor/core/backend.ts b/src/editor/core/backend.tsindex a1b294ef8..4286a448b 100644--- a/src/editor/core/backend.ts+++ b/src/editor/core/backend.ts@@ -138,6 +138,7 @@ const EXT_LANG: Record<string, string> = {md: "markdown",adoc: "asciidoc",asciidoc: "asciidoc",+ rst: "restructuredtext",css: "css",html: "html",yml: "yaml",@@ -246,6 +247,15 @@ const MEM_LANGUAGES: LanguageInfo[] = [blockComment: null,indent: { tabSize: 2, insertSpaces: true },},+ {+ id: "restructuredtext",+ name: "reStructuredText",+ extensions: ["rst"],+ uiMode: "reStructuredText",+ lineComment: "..",+ blockComment: null,+ indent: { tabSize: 3, insertSpaces: true },+ },{id: "markdown",name: "Markdown",diff --git a/src/editor/core/cm/comments.ts b/src/editor/core/cm/comments.tsindex 6c67f7a84..a58667e7e 100644--- a/src/editor/core/cm/comments.ts+++ b/src/editor/core/cm/comments.ts@@ -29,6 +29,10 @@ function lineToken(state: EditorState, pos: number): string | undefined {return state.languageDataAt<{ line?: string }>("commentTokens", pos)[0]?.line;}+function startsLineComment(text: string, token: string): boolean {+ return text.startsWith(token) && (token !== ".." || text.length === 2 || /^[ \t]/.test(text.slice(2)));+}+const leadingWs = (text: string) => /^[ \t]*/.exec(text)![0].length;interface Block {@@ -86,7 +90,7 @@ export const commentLine: Command = (view) => {const single = b.startLine === b.endLine;for (let n = b.startLine; n <= b.endLine; n++) {const text = doc.line(n).text;- const commented = text.slice(leadingWs(text)).startsWith(token);+ const commented = startsLineComment(text.slice(leadingWs(text)), token);const empty = !text.trim();if (commented && !empty) commentedNonEmpty = true;if (!commented && (single || !empty)) allCommented = false;@@ -97,13 +101,13 @@ export const commentLine: Command = (view) => {const changes: ChangeSpec[] = [];blocks.forEach((b, bi) => {const token = tokens[bi]!;- // A word token (Batch `@REM`) needs a separator even when comment spacing is off.- const addSpace = style.addSpace || /\w$/.test(token);+ // A word token (Batch `@REM`) and reStructuredText's `..` need a separator even when comment spacing is off.+ const addSpace = style.addSpace || /\w$|^\.\.$/.test(token);if (allCommented) {for (let n = b.startLine; n <= b.endLine; n++) {const line = doc.line(n);const start = line.from + leadingWs(line.text);- if (!doc.sliceString(start, line.to).startsWith(token) || !line.text.trim()) continue;+ if (!startsLineComment(doc.sliceString(start, line.to), token) || !line.text.trim()) continue;let end = start + token.length;if (addSpace && doc.sliceString(end, end + 1) === " ") end++;// Nothing but whitespace left: the line is emptied.@@ -191,7 +195,7 @@ function minIndent(state: EditorState, startLine: number, endLine: number, tokenif (min === null && startLine === endLine && startLine < doc.lines) scan(startLine + 1, startLine + 1);if (startLine > 1) {const above = doc.line(startLine - 1).text;- if (above.slice(leadingWs(above)).startsWith(token)) min = Math.min(min ?? Infinity, indentOf(above));+ if (startsLineComment(above.slice(leadingWs(above)), token)) min = Math.min(min ?? Infinity, indentOf(above));}return min ?? 0;}diff --git a/src/editor/core/cm/editing.test.ts b/src/editor/core/cm/editing.test.tsindex a9b6edcd6..7fdb746e9 100644--- a/src/editor/core/cm/editing.test.ts+++ b/src/editor/core/cm/editing.test.ts@@ -15,6 +15,7 @@ import {} from "./editing";import { batchLanguage, commentTokens, findLanguageDescription } from "./languages";import { toggleCaret } from "./multiCaret";+import { restructuredtextLanguage } from "./restructuredtext";import { commentBlock, enterInLineComment, lineCommentStyle } from "./comments";import { languages } from "@codemirror/language-data";@@ -339,6 +340,32 @@ describe("fallback indentation boundaries", () => {});});+describe("reStructuredText comment toggle", () => {+ it("comments with `.. `, since `..text` is not a comment", () => {+ const v = make("Some| text\n", restructuredtextLanguage);+ commentLine(v);+ expect(v.state.doc.toString()).toBe(".. Some text\n");+ v.dispatch({ selection: EditorSelection.cursor(0) });+ commentLine(v);+ expect(v.state.doc.toString()).toBe("Some text\n");+ });++ it.each(["..text", "...", "..中文"])("preserves the dots in ordinary text %s", (text) => {+ const v = make(`|${text}\n`, restructuredtextLanguage);+ commentLine(v);+ expect(v.state.doc.toString()).toBe(`.. ${text}\n`);+ v.dispatch({ selection: EditorSelection.cursor(0) });+ commentLine(v);+ expect(v.state.doc.toString()).toBe(`${text}\n`);+ });++ it.each(["..", ".. comment", "..\tcomment"])("uncomments a valid prefix in %s", (text) => {+ const v = make(`|${text}\n`, restructuredtextLanguage);+ commentLine(v);+ expect(v.state.doc.toString()).toBe(`${text.slice(2).replace(/^ /, "")}\n`);+ });+});+describe("Batch language mode and @REM comment toggle", () => {it("resolves Batch by uiMode and .bat / .cmd extensions and toggles @REM comments", () => {expect(findLanguageDescription("Batch", "build.bat")?.name).toBe("Batch");diff --git a/src/editor/core/cm/languages.test.ts b/src/editor/core/cm/languages.test.tsindex c5a7a354b..33cf41cc2 100644--- a/src/editor/core/cm/languages.test.ts+++ b/src/editor/core/cm/languages.test.ts@@ -1,3 +1,4 @@+import { EditorState } from "@codemirror/state";import { describe, expect, it } from "vitest";import { highlightTree, tagHighlighter, tags as t } from "@lezer/highlight";import { logWarning } from "./highlight";@@ -136,6 +137,123 @@ describe("language modes", () => {]);});+ it("recognizes reStructuredText files and highlights their syntax", async () => {+ expect(findLanguageDescription("reStructuredText", "/docs/index.rst")?.name).toBe("reStructuredText");+ const support = await loadSupport(findLanguageDescription("reStructuredText", "/docs/index.rst")!);+ const state = EditorState.create({ extensions: support!.language });+ expect(state.languageDataAt("commentTokens", 0)).toEqual([{ line: ".." }]);+ const doc = [+ "=====",+ "Guide",+ "=====",+ "",+ "Intro",+ "-----",+ "",+ "Some **bold**, *em*, ``code``, :py:func:`len`, `Docs <https://example.com>`_ and target_.",+ "See https://example.com and |sub|.",+ "",+ "Example::",+ "",+ " Not a title",+ " -----------",+ "",+ ".. code-block:: python",+ " :linenos:",+ "",+ " x = 1",+ "",+ ".. _target:",+ "",+ ".. a comment",+ " continued",+ "",+ ":field: value",+ "",+ "A paragraph",+ "-----",+ "",+ "Two lines of",+ "a paragraph",+ "------------",+ ].join("\n");+ const highlighter = tagHighlighter([+ { tag: t.heading, class: "heading" },+ { tag: t.strong, class: "strong" },+ { tag: t.emphasis, class: "emphasis" },+ { tag: t.monospace, class: "monospace" },+ { tag: t.keyword, class: "keyword" },+ { tag: t.string, class: "string" },+ { tag: t.link, class: "link" },+ { tag: t.url, class: "url" },+ { tag: t.variableName, class: "variable" },+ { tag: t.attributeName, class: "attribute" },+ { tag: t.labelName, class: "label" },+ { tag: t.comment, class: "comment" },+ ]);+ const highlighted: [string, string][] = [];+ highlightTree(support!.language.parser.parse(doc), highlighter, (from, to, style) => {+ highlighted.push([doc.slice(from, to), style]);+ });+ expect(highlighted).toEqual([+ ["=====\nGuide\n=====", "heading"],+ ["Intro\n-----", "heading"],+ ["**bold**", "strong"],+ ["*em*", "emphasis"],+ ["``code``", "monospace"],+ [":py:func:", "keyword"],+ ["`len`", "string"],+ ["`Docs <https://example.com>`_", "link"],+ ["target_", "link"],+ ["https://example.com", "url"],+ ["|sub|", "variable"],+ ["Not a title", "monospace"],+ ["-----------", "monospace"],+ [".. code-block::", "keyword"],+ [":linenos:", "attribute"],+ ["x = 1", "monospace"],+ [".. _target:", "label"],+ [".. a comment", "comment"],+ ["continued", "comment"],+ [":field:", "attribute"],+ ["A paragraph\n-----", "heading"],+ ]);+ });++ it("preserves Unicode heading offsets with CRLF and skips escaped markup", async () => {+ const support = await loadSupport(findLanguageDescription("reStructuredText", "guide.rst")!);+ const doc =+ "=========\r\n 中文😀\r\n=========\r\n\r\n子章节\r\n------\r\n\r\n\\*plain* and \\`plain` and **strong**";+ const highlighted: [string, string][] = [];+ highlightTree(+ support!.language.parser.parse(doc),+ tagHighlighter([+ { tag: t.heading, class: "heading" },+ { tag: t.strong, class: "strong" },+ { tag: t.emphasis, class: "emphasis" },+ { tag: t.string, class: "string" },+ ]),+ (from, to, style) => highlighted.push([doc.slice(from, to), style]),+ );+ expect(highlighted).toEqual([+ ["=========\r\n 中文😀\r\n=========", "heading"],+ ["子章节\r\n------", "heading"],+ ["**strong**", "strong"],+ ]);+ });++ it("counts the indentation of short overlined titles", async () => {+ const support = await loadSupport(findLanguageDescription("reStructuredText", "guide.rst")!);+ const doc = "===\n Hi\n===";+ const headings: string[] = [];+ highlightTree(+ support!.language.parser.parse(doc),+ tagHighlighter([{ tag: t.heading, class: "heading" }]),+ (from, to) => headings.push(doc.slice(from, to)),+ );+ expect(headings).toEqual([]);+ });+it("styles `?.` like `.` in JavaScript and TypeScript", async () => {const highlighter = tagHighlighter([{ tag: t.derefOperator, class: "deref" }]);for (const [mode, path] of [diff --git a/src/editor/core/cm/languages.ts b/src/editor/core/cm/languages.tsindex 54e66b58f..98992f97b 100644--- a/src/editor/core/cm/languages.ts+++ b/src/editor/core/cm/languages.ts@@ -87,6 +87,11 @@ const OWN_MODES = [extensions: ["adoc", "asciidoc"],load: () => import("./asciidoc").then((m) => new LanguageSupport(m.asciidocLanguage)),}),+ LanguageDescription.of({+ name: "reStructuredText",+ extensions: ["rst"],+ load: () => import("./restructuredtext").then((m) => new LanguageSupport(m.restructuredtextLanguage)),+ }),LanguageDescription.of({name: "Log",extensions: ["log"],@@ -328,7 +333,16 @@ const DEDENT_STATEMENT = /^\s*(?:pass|break|continue|return)\b/;const KEYWORD_OPEN_LANGS = new Set(["elixir", "ruby", "lua"]);const KEYWORD_OPEN =/\b(?:do(?:\s*\|[^|]*\|)?|then|->)$|^\s*(?:def|defp|defmodule|class|module|function|local\s+function|if|unless|elsif|elseif|else|while|until|for|case|begin|cond|with|receive|try)\b/;-const NO_INDENT_LANGS = new Set(["plaintext", "markdown", "asciidoc", "log", "diff", "ignore", "gitattributes"]);+const NO_INDENT_LANGS = new Set([+ "plaintext",+ "markdown",+ "asciidoc",+ "restructuredtext",+ "log",+ "diff",+ "ignore",+ "gitattributes",+]);function opensBlock(code: string, language: string | undefined): boolean {if (!code) return false;diff --git a/src/editor/core/cm/restructuredtext.ts b/src/editor/core/cm/restructuredtext.tsnew file mode 100644index 000000000..cf1a41de0--- /dev/null+++ b/src/editor/core/cm/restructuredtext.ts@@ -0,0 +1,169 @@+import { defineLanguageFacet, Language, languageDataProp } from "@codemirror/language";+import { type Input, NodeSet, NodeType, Parser, type PartialParse, Tree } from "@lezer/common";+import { styleTags, tags as t } from "@lezer/highlight";++// A flat token list instead of a StreamLanguage: an underlined section title is+// only known from the line after it, and stream modes cannot look ahead.+const STYLES = {+ heading: t.heading,+ strong: t.strong,+ emphasis: t.emphasis,+ monospace: t.monospace,+ role: t.keyword,+ interpreted: t.string,+ link: t.link,+ url: t.url,+ substitution: t.special(t.variableName),+ field: t.attributeName,+ label: t.labelName,+ directive: t.keyword,+ comment: t.comment,+};+type Style = keyof typeof STYLES;+const NAMES = ["Document", ...Object.keys(STYLES)];+const data = defineLanguageFacet({ commentTokens: { line: ".." } });+const nodeSet = new NodeSet(+ NAMES.map((name, id) =>+ NodeType.define({ id, name, top: id === 0, props: id === 0 ? [[languageDataProp, data]] : [] }),+ ),+).extend(styleTags(STYLES));++/** An indented body (after `::`, a code directive or `..`) lasting while lines are indented past `base`. */+type Block = { kind: "literal" | "code" | "comment"; base: number; options: boolean };++const ADORNMENT = /^([!-/:-@[-`{-~])\1*\s*$/;+const CODE_DIRECTIVES = new Set(["code", "code-block", "sourcecode"]);+// Lines docutils reads as another body element before trying a title.+const BODY_ELEMENT = /^(?:\.\.|__|[-*+•]|>>>|\|)(?:\s|$)|^:[^:\s][^:]*:(?:\s|$)/;+// Inline markup only starts after whitespace or opening punctuation.+const MARKUP_START = /[\s'"([{<\-/:]/;+const INLINE: [RegExp, Style][] = [+ [/``\S(?:.*?\S)?``/y, "monospace"],+ [/\*\*\S(?:.*?\S)?\*\*/y, "strong"],+ [/\*\S(?:.*?\S)?\*/y, "emphasis"],+ [/:[\w.+-]+(?::[\w.+-]+)*:(?=`)/y, "role"],+ [/`[^`]+`__?/y, "link"],+ [/`[^`]+`/y, "interpreted"],+ [/\|\S(?:[^|]*\S)?\|(?:__?)?/y, "substitution"],+ [/\[(?:\d+|#[\w-]*|\*|[\w.-]+)\]_/y, "link"],+ [/https?:\/\/[^\s<>`]*[^\s<>`.,;:!?)\]'"]/y, "url"],+ [/[A-Za-z0-9]+(?:[-_.:+][A-Za-z0-9]+)*__?(?!\w)/y, "link"],+];+const WORD = /\w+/y;++const adornment = (line: string) => (ADORNMENT.test(line) ? line.trimEnd() : null);+/** docutils accepts a short adornment (with a warning) unless it is under 4 characters. */+const fits = (title: string, adornment: string) => adornment.length >= Math.min(4, [...title.trimEnd()].length);++/** `[nodeId, from, to, 4]` for each token, in document order (a lezer tree buffer). */+function tokenize(text: string): number[] {+ const out: number[] = [];+ const add = (style: Style, from: number, to: number) => {+ if (to > from) out.push(NAMES.indexOf(style), from, to, 4);+ };+ const lines = text.split("\n");+ const starts: number[] = [];+ for (let i = 0, at = 0; i < lines.length; at += lines[i]!.length + 1, i++) starts.push(at);+ const end = (i: number) => starts[i]! + lines[i]!.trimEnd().length;++ let block: Block | null = null;+ let inPara = false;+ for (let i = 0; i < lines.length; i++) {+ const line = lines[i]!;+ const off = starts[i]!;+ const indent = line.length - line.trimStart().length;+ if (!line.trim()) {+ inPara = false;+ if (block) block.options = false;+ continue;+ }+ if (block) {+ if (indent > block.base) {+ const option = block.kind === "code" && block.options ? /^\s*:[\w-]+:/.exec(line) : null;+ if (option) add("field", off + indent, off + option[0].length);+ else add(block.kind === "comment" ? "comment" : "monospace", off + indent, end(i));+ continue;+ }+ block = null;+ }+ const over = adornment(line);+ if (!inPara) {+ const [next, after] = [lines[i + 1] ?? "", lines[i + 2] ?? ""];+ if (over && next.trim() && !adornment(next) && adornment(after) === over && fits(next, over)) {+ add("heading", off, end(i + 2));+ i += 2;+ continue;+ }+ const underline = !over && indent === 0 && !BODY_ELEMENT.test(line) ? adornment(next) : null;+ if (underline && fits(line, underline)) {+ add("heading", off, end(i + 1));+ i += 1;+ continue;+ }+ }+ if (over && over.length >= 4 && !inPara) {+ // A transition, or an adornment that matched no title (shorter ones, like `::`, are text).+ add("heading", off, end(i));+ inPara = false;+ continue;+ }+ inPara = true;++ let p = 0;+ const directive = /^\s*\.\.\s+(?:\|[^|]+\|\s+)?([\w:+.-]+)::/.exec(line);+ const target = /^\s*(?:\.\.\s+(?:_[^:]*:|\[[^\]]+\])|__(?=\s))/.exec(line);+ if (directive) {+ if (CODE_DIRECTIVES.has(directive[1]!)) block = { kind: "code", base: indent, options: true };+ add("directive", off + indent, off + directive[0].length);+ p = directive[0].length;+ } else if (target) {+ add("label", off + indent, off + target[0].length);+ p = target[0].length;+ } else if (/^\s*\.\.(?:\s|$)/.test(line)) {+ add("comment", off + indent, end(i));+ block = { kind: "comment", base: indent, options: false };+ continue;+ } else {+ const field = /^\s*:[^:\s][^:]*:(?=\s|$)/.exec(line);+ if (field) add("field", off + indent, off + field[0].length);+ p = field ? field[0].length : 0;+ if (line.trimEnd().endsWith("::")) block = { kind: "literal", base: indent, options: false };+ }++ while (p < line.length) {+ let matched = 0;+ if (MARKUP_START.test(p === 0 ? " " : line[p - 1]!)) {+ for (const [re, style] of INLINE) {+ re.lastIndex = p;+ const m = re.exec(line);+ if (m) {+ add(style, off + p, off + p + m[0].length);+ matched = m[0].length;+ break;+ }+ }+ }+ WORD.lastIndex = p;+ p += matched || WORD.exec(line)?.[0].length || 1;+ }+ }+ return out;+}++// ponytail: reparses the whole document on each change (no fragment reuse);+// fine for documentation-sized files, add incremental reuse if large ones lag.+class RstParser extends Parser {+ createParse(input: Input): PartialParse {+ return {+ parsedPos: input.length,+ stoppedAt: null,+ stopAt() {},+ advance: () => {+ const text = input.read(0, input.length);+ return Tree.build({ buffer: tokenize(text), nodeSet, topID: 0, length: text.length });+ },+ };+ }+}++export const restructuredtextLanguage = new Language(data, new RstParser(), [], "restructuredtext");