diff --git a/src/editor/core/cm/smartKeys.test.ts b/src/editor/core/cm/smartKeys.test.ts index 62d387f97..a115414c8 100644 --- a/src/editor/core/cm/smartKeys.test.ts +++ b/src/editor/core/cm/smartKeys.test.ts @@ -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", () => { diff --git a/src/editor/core/cm/smartKeys.ts b/src/editor/core/cm/smartKeys.ts index 4c02a82c6..7de10970c 100644 --- a/src/editor/core/cm/smartKeys.ts +++ b/src/editor/core/cm/smartKeys.ts @@ -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["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 boolean> = { java: javaTypePosition, typescript: tsTypePosition, @@ -367,6 +449,10 @@ const ANGLE_PAIR_RULES: Record 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. */