дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-04 09:11:10 +02:00
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 <[email protected]>
Цей коміт міститься в:
1 джерело
4237117f2c
коміт
db0cdccd66
2 files changed
+93
-2
No files matched your search
@@ -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 = "<?php class Palette extends BasePalette { var $name; function example() {} }";
|
||||
|
||||
@@ -12,6 +12,8 @@ import {
|
||||
LanguageSupport,
|
||||
StreamLanguage,
|
||||
delimitedIndent,
|
||||
foldInside,
|
||||
foldNodeProp,
|
||||
type Language,
|
||||
indentNodeProp,
|
||||
indentService,
|
||||
@@ -137,6 +139,20 @@ export function languagesForPath(path: string): readonly LanguageInfo[] | undefi
|
||||
return [...found];
|
||||
}
|
||||
|
||||
/**
|
||||
* Folds the SELECT part of a multi-line SQL statement: from the end of the line with the
|
||||
* statement's own `SELECT` keyword (not one inside a subquery) to the statement's end, so the
|
||||
* `SELECT …` line and an `INSERT … SELECT`'s INSERT line stay visible.
|
||||
*/
|
||||
function foldSelectPart(node: SyntaxNode, state: EditorState) {
|
||||
let select: SyntaxNode | null = null;
|
||||
for (let child = node.firstChild; child && !select; child = child.nextSibling)
|
||||
if (child.name === "Keyword" && state.sliceDoc(child.from, child.to).toLowerCase() === "select") select = child;
|
||||
if (!select) return null;
|
||||
const line = state.doc.lineAt(select.from);
|
||||
return line.to < node.to && line.number !== state.doc.lineAt(node.to).number ? { from: line.to, to: node.to } : null;
|
||||
}
|
||||
|
||||
const supports = new Map<string, Promise<LanguageSupport | null>>();
|
||||
|
||||
/** 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);
|
||||
},
|
||||
}),
|
||||
|
||||
Reference in new issue
Заблокувати користувача