From db0cdccd6631b84c5305296281efdd8b6f8dd691 Mon Sep 17 00:00:00 2001 From: LeoChen8118 <15079184757@163.com> Date: Thu, 3 Dec 2026 16:29:34 +0800 Subject: [PATCH] feat(sql): fold subqueries and the SELECT part of a statement (#7671) SQL files had no folding. Parenthesised expressions (subqueries, IN (...), EXISTS (...)) fold like a bracket block, and a multi-line statement folds from the end of its own SELECT line to its end, so the SELECT line and an INSERT ... SELECT's INSERT line stay visible. The statement's SELECT is taken from the syntax tree, so a subquery's SELECT folds only its parentheses. Co-authored-by: LeoChen8118 <169817173+LeoChen8118@users.noreply.github.com> --- src/editor/core/cm/languages.test.ts | 70 +++++++++++++++++++++++++++- src/editor/core/cm/languages.ts | 25 +++++++++- 2 files changed, 93 insertions(+), 2 deletions(-) diff --git a/src/editor/core/cm/languages.test.ts b/src/editor/core/cm/languages.test.ts index c82f13e5c2..bf26510263 100644 --- a/src/editor/core/cm/languages.test.ts +++ b/src/editor/core/cm/languages.test.ts @@ -1,7 +1,7 @@ import { EditorSelection, EditorState } from "@codemirror/state"; import { EditorView } from "@codemirror/view"; import { insertNewlineAndIndent } from "@codemirror/commands"; -import { LanguageDescription, indentUnit, type Language } from "@codemirror/language"; +import { LanguageDescription, ensureSyntaxTree, foldable, indentUnit, type Language } from "@codemirror/language"; import { describe, expect, it } from "vitest"; import { getIndentation } from "@codemirror/language"; import { highlightTree, tagHighlighter, tags as t } from "@lezer/highlight"; @@ -83,6 +83,74 @@ describe("language modes", () => { ]); }); + it("folds SQL subquery parentheses", async () => { + const support = await loadSupport(findLanguageDescription("SQL", "/tmp/data.sql")!); + const doc = [ + "SELECT a", + "FROM t", + "WHERE x IN (", + " SELECT y FROM s WHERE z > 1", + ")", + "AND EXISTS (", + " SELECT 1 FROM u WHERE u.a = t.a", + ")", + ";", + ].join("\n"); + const state = EditorState.create({ doc, extensions: [support!.language] }); + ensureSyntaxTree(state, doc.length, 1000); + // The `IN (...)` subquery folds from the line that opens the parentheses to the matching close. + const inLine = state.doc.line(3); + const inFold = foldable(state, inLine.from, inLine.to)!; + expect(state.doc.lineAt(inFold.from).text.trim()).toBe("WHERE x IN ("); + expect(state.doc.lineAt(inFold.to).text.trim()).toBe(")"); + // The `EXISTS (...)` subquery folds the same way. + const existsLine = state.doc.line(6); + const existsFold = foldable(state, existsLine.from, existsLine.to)!; + expect(state.doc.lineAt(existsFold.from).text.trim()).toBe("AND EXISTS ("); + expect(state.doc.lineAt(existsFold.to).text.trim()).toBe(")"); + // A plain one-line value list (`VALUES (…)`) is not folded. + const valuesState = EditorState.create({ + doc: "INSERT INTO t VALUES (1, 2, 3);", + extensions: [support!.language], + }); + ensureSyntaxTree(valuesState, valuesState.doc.length, 1000); + const vLine = valuesState.doc.line(1); + expect(foldable(valuesState, vLine.from, vLine.to)).toBeNull(); + }); + + it("folds the SELECT part of a statement", async () => { + const support = await loadSupport(findLanguageDescription("SQL", "/tmp/data.sql")!); + // A standalone multi-line SELECT folds as a whole from its first line. + const selectState = EditorState.create({ + doc: "SELECT x, y\nFROM s\nWHERE z > 1;", + extensions: [support!.language], + }); + ensureSyntaxTree(selectState, selectState.doc.length, 1000); + const selectFold = foldable(selectState, selectState.doc.line(1).from, selectState.doc.line(1).to)!; + expect(selectState.doc.lineAt(selectFold.from).text.trim()).toBe("SELECT x, y"); + expect(selectState.doc.lineAt(selectFold.to).text.trim()).toBe("WHERE z > 1;"); + + // An `INSERT ... SELECT` folds only the SELECT part, keeping the INSERT header visible. + const insertState = EditorState.create({ + doc: "INSERT INTO t (a, b)\nSELECT x, y\nFROM s\nWHERE z > 1;", + extensions: [support!.language], + }); + ensureSyntaxTree(insertState, insertState.doc.length, 1000); + const insertFold = foldable(insertState, insertState.doc.line(2).from, insertState.doc.line(2).to)!; + expect(insertState.doc.lineAt(insertFold.from).text.trim()).toBe("SELECT x, y"); + // The INSERT line itself has no fold. + expect(foldable(insertState, insertState.doc.line(1).from, insertState.doc.line(1).to)).toBeNull(); + + // A SELECT inside a subquery is not the statement's own: only its parentheses fold. + const updateState = EditorState.create({ + doc: "UPDATE t\nSET a = 1\nWHERE id IN (SELECT id\n FROM s);", + extensions: [support!.language], + }); + ensureSyntaxTree(updateState, updateState.doc.length, 1000); + const subquery = foldable(updateState, updateState.doc.line(3).from, updateState.doc.line(3).to)!; + expect(updateState.sliceDoc(subquery.from, subquery.to)).toBe("SELECT id\n FROM s"); + }); + it("distinguishes PHP classes, method declarations and fields for imported schemes", async () => { const support = await loadSupport(findLanguageDescription("PHP", "/tmp/palette.php")!); const source = ">(); /** Frontend modes that extend or replace the bundled parsers. */ @@ -148,7 +164,14 @@ const OWN_MODES = [ const { sql, StandardSQL } = await import("@codemirror/lang-sql"); const support = sql({ dialect: StandardSQL }); // lang-sql tags `"public"."t"` like a string; it names a schema and a table, as IDEA colours it. - const language = (support.language as LRLanguage).configure({ props: [styleTags({ QuotedIdentifier: t.name })] }); + // Its `Parens` node wraps every parenthesised expression (subqueries, `IN (...)`, `VALUES (...)`), + // which has no backend tree-sitter grammar, so fold it like IDEA folds a bracket block. + const language = (support.language as LRLanguage).configure({ + props: [ + styleTags({ QuotedIdentifier: t.name }), + foldNodeProp.add({ Parens: foldInside, Statement: foldSelectPart }), + ], + }); return new LanguageSupport(language, support.support); }, }),