mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 16:04:21 +00:00
fix(editor): pair include and template angle brackets in C and C++ (#5126)
With bracket pairing on, `<` after `#include ` (C, C++) and after `std::vector` (C++) stayed single: `smartAngleBrackets` had no rule for C or C++. - C and Objective-C: `#include <` (and `#include_next`, `#import`) pairs. - C++ and Objective-C++: the same, plus `template <`, a standard template or cast written against its `<` (`std::vector<`, `std::make_unique<`, `static_cast<`…) and a capitalized name (`Box<`). - Lowercase values (`a <`, `i<`), `std::cout <`, all-caps macros (`MAX<`) and `operator<` stay single. `inLiteral` treated any node whose name contains "template" as a template literal, which made Lezer C++'s `TemplateFunction` (`std::make_unique`) look like a string; C++ template nodes are now excluded.
This commit is contained in:
@@ -352,8 +352,27 @@ describe("smart angle brackets", () => {
|
||||
const KOTLIN = StreamLanguage.define(interpolationParser(kotlin, "Kotlin"));
|
||||
const CSHARP = StreamLanguage.define(interpolationParser(csharp, "C#"));
|
||||
let RUST: LanguageSupport;
|
||||
let CPP: LanguageSupport;
|
||||
beforeAll(async () => {
|
||||
RUST = (await cmLanguages.find((l) => l.name === "Rust")!.load()) as LanguageSupport;
|
||||
CPP = (await cmLanguages.find((l) => l.name === "C++")!.load()) as LanguageSupport;
|
||||
});
|
||||
|
||||
it("pairs include brackets in C and C++, and template arguments in C++", () => {
|
||||
pair("c", CPP, "#include |", "#include <|>");
|
||||
pair("cpp", CPP, "#include |", "#include <|>");
|
||||
pair("cpp", CPP, "std::vector|", "std::vector<|>");
|
||||
pair("cpp", CPP, "auto p = std::make_unique|", "auto p = std::make_unique<|>");
|
||||
pair("cpp", CPP, "auto n = static_cast|", "auto n = static_cast<|>");
|
||||
pair("cpp", CPP, "template |", "template <|>");
|
||||
pair("cpp", CPP, "Box|", "Box<|>");
|
||||
});
|
||||
|
||||
it("does not pair comparisons, shifts or macros in C and C++", () => {
|
||||
pair("c", CPP, "int f() { return a |", "int f() { return a <|");
|
||||
pair("cpp", CPP, "for (int i = 0; i|", "for (int i = 0; i<|");
|
||||
pair("cpp", CPP, "std::cout |", "std::cout <|");
|
||||
pair("cpp", CPP, "if (x < MAX|", "if (x < MAX<|");
|
||||
});
|
||||
|
||||
it("pairs after a type name in Java: fields, type arguments and class declarations", () => {
|
||||
|
||||
@@ -273,13 +273,16 @@ function wordBefore(state: EditorState, pos: number): string {
|
||||
}
|
||||
|
||||
/** The caret is inside a literal: never pair there, whatever the language rule says. */
|
||||
/** C++ template syntax, not a template literal. */
|
||||
const CPP_TEMPLATE_NODE = /^Template(Function|Type|Method|ArgumentList|ParameterList|Declaration|Instantiation)$/;
|
||||
|
||||
function inLiteral(state: EditorState, pos: number): boolean {
|
||||
for (
|
||||
let n: ReturnType<ReturnType<typeof syntaxTree>["resolveInner"]> | null = syntaxTree(state).resolveInner(pos, -1);
|
||||
n;
|
||||
n = n.parent
|
||||
)
|
||||
if (/string|comment|template|rune/i.test(n.name)) return true;
|
||||
if (/string|comment|template|rune/i.test(n.name) && !CPP_TEMPLATE_NODE.test(n.name)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -360,6 +363,85 @@ function cLikeTypePosition(state: EditorState, pos: number): boolean {
|
||||
);
|
||||
}
|
||||
|
||||
/** Standard C++ templates (and casts) written against their `<`. */
|
||||
const CPP_TEMPLATES = new Set([
|
||||
"vector",
|
||||
"array",
|
||||
"deque",
|
||||
"list",
|
||||
"forward_list",
|
||||
"map",
|
||||
"multimap",
|
||||
"set",
|
||||
"multiset",
|
||||
"unordered_map",
|
||||
"unordered_multimap",
|
||||
"unordered_set",
|
||||
"unordered_multiset",
|
||||
"stack",
|
||||
"queue",
|
||||
"priority_queue",
|
||||
"pair",
|
||||
"tuple",
|
||||
"optional",
|
||||
"variant",
|
||||
"any",
|
||||
"function",
|
||||
"span",
|
||||
"basic_string",
|
||||
"unique_ptr",
|
||||
"shared_ptr",
|
||||
"weak_ptr",
|
||||
"make_unique",
|
||||
"make_shared",
|
||||
"atomic",
|
||||
"future",
|
||||
"promise",
|
||||
"initializer_list",
|
||||
"numeric_limits",
|
||||
"is_same",
|
||||
"enable_if",
|
||||
"conditional",
|
||||
"decay",
|
||||
"remove_reference",
|
||||
"static_cast",
|
||||
"dynamic_cast",
|
||||
"const_cast",
|
||||
"reinterpret_cast",
|
||||
"duration_cast",
|
||||
"get",
|
||||
"hash",
|
||||
"less",
|
||||
"greater",
|
||||
"allocator",
|
||||
"reference_wrapper",
|
||||
"string_view",
|
||||
"bitset",
|
||||
"complex",
|
||||
"valarray",
|
||||
]);
|
||||
|
||||
/** `#include <` in C and C++. */
|
||||
function includePosition(state: EditorState, pos: number): boolean {
|
||||
const line = state.doc.lineAt(pos);
|
||||
return /^\s*#\s*(include|include_next|import)\s*$/.test(line.text.slice(0, pos - line.from));
|
||||
}
|
||||
|
||||
/**
|
||||
* C++: `#include <`, `template <`, and a template name written against its `<`: a standard one
|
||||
* (`std::vector<`, `static_cast<`) or a capitalized one (`Box<`), never a value (`a < b`, `i<n`)
|
||||
* or an all-caps macro.
|
||||
*/
|
||||
function cppTypePosition(state: EditorState, pos: number): boolean {
|
||||
if (includePosition(state, pos)) return true;
|
||||
const line = state.doc.lineAt(pos);
|
||||
const before = line.text.slice(0, pos - line.from);
|
||||
if (/\btemplate\s*$/.test(before)) return true;
|
||||
const name = /(?:^|[^\w])(\w+)$/.exec(before)?.[1];
|
||||
if (!name || /\boperator$/.test(before)) return false;
|
||||
return CPP_TEMPLATES.has(name) || (/^[A-Z]/.test(name) && /[a-z]/.test(name));
|
||||
}
|
||||
|
||||
const ANGLE_PAIR_RULES: Record<string, (state: EditorState, pos: number) => boolean> = {
|
||||
java: javaTypePosition,
|
||||
typescript: tsTypePosition,
|
||||
@@ -367,6 +449,10 @@ const ANGLE_PAIR_RULES: Record<string, (state: EditorState, pos: number) => bool
|
||||
kotlin: cLikeTypePosition,
|
||||
csharp: cLikeTypePosition,
|
||||
rust: rustTypePosition,
|
||||
c: includePosition,
|
||||
cpp: cppTypePosition,
|
||||
"objective-c": includePosition,
|
||||
"objective-cpp": cppTypePosition,
|
||||
};
|
||||
|
||||
/** The `<>` pairs the smart angle brackets inserted; `>` steps over their closing half. */
|
||||
|
||||
Reference in New Issue
Block a user