From f202355048c8d915c6978f6e9300dd63104b8a52 Mon Sep 17 00:00:00 2001 From: Noah Peart Date: Fri, 19 Apr 2024 01:46:50 -0700 Subject: [PATCH] Add typescript-ts-mode indentation for multi-assignment decls * lisp/progmodes/typescript-ts-mode.el (typescript-ts-mode--indent-rules): Add indentation rules for lexical and variable declarations with multiple assignments. * test/lisp/progmodes/typescript-ts-mode-resources/indent.erts: Add indent test for variable declarations. --- .last-build-hash | 1 + lisp/progmodes/typescript-ts-mode.el | 15 +++++++++- .../typescript-ts-mode-resources/indent.erts | 28 +++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 .last-build-hash diff --git a/.last-build-hash b/.last-build-hash new file mode 100644 index 00000000000..77fe4446df5 --- /dev/null +++ b/.last-build-hash @@ -0,0 +1 @@ +a2e327cbca1e756373109d4788ea635250d23224 diff --git a/lisp/progmodes/typescript-ts-mode.el b/lisp/progmodes/typescript-ts-mode.el index ab1d76ab20e..3c10a19e712 100644 --- a/lisp/progmodes/typescript-ts-mode.el +++ b/lisp/progmodes/typescript-ts-mode.el @@ -91,6 +91,17 @@ tsx-ts-mode--indent-compatibility-b893426 `(((match "<" "jsx_text") parent 0) ((parent-is "jsx_text") parent typescript-ts-mode-indent-offset))))) +(defun typescript-ts-mode--anchor-decl (_n parent &rest _) + "Return the position after the declaration keyword before PARENT. + +This anchor allows aligning variable_declarators in variable and lexical +declarations, accounting for the length of keyword (var, let, or const)." + (let* ((declaration (treesit-parent-until + parent (rx (or "variable" "lexical") "_declaration") t)) + (decl (treesit-node-child declaration 0))) + (+ (treesit-node-start declaration) + (- (treesit-node-end decl) (treesit-node-start decl))))) + (defun typescript-ts-mode--indent-rules (language) "Rules used for indentation. Argument LANGUAGE is either `typescript' or `tsx'." @@ -113,7 +124,9 @@ typescript-ts-mode--indent-rules ((parent-is "switch_case") parent-bol typescript-ts-mode-indent-offset) ((parent-is "switch_default") parent-bol typescript-ts-mode-indent-offset) ((parent-is "type_arguments") parent-bol typescript-ts-mode-indent-offset) - ((parent-is "variable_declarator") parent-bol typescript-ts-mode-indent-offset) + ((parent-is "variable_declarator") grand-parent typescript-ts-mode-indent-offset) + ((parent-is ,(rx (or "variable" "lexical") "_declaration")) + typescript-ts-mode--anchor-decl 1) ((parent-is "arguments") parent-bol typescript-ts-mode-indent-offset) ((parent-is "array") parent-bol typescript-ts-mode-indent-offset) ((parent-is "formal_parameters") parent-bol typescript-ts-mode-indent-offset) diff --git a/test/lisp/progmodes/typescript-ts-mode-resources/indent.erts b/test/lisp/progmodes/typescript-ts-mode-resources/indent.erts index bec96ad82e0..908dd8103b5 100644 --- a/test/lisp/progmodes/typescript-ts-mode-resources/indent.erts +++ b/test/lisp/progmodes/typescript-ts-mode-resources/indent.erts @@ -62,6 +62,34 @@ const foo = (x: string) => { }; =-=-= +Name: Lexical and variable declarations + +=-= +const foo = () => { + let x = 1, + yyyy: { + [k: string | number]: string, + } = { + "foo": "foo", + "bar": "bar", + }; + var obar = 1, + fo: { [x: any]: any } = { + "a": 1, + "b": 2, + }; + const someFuncWithReallyLongName = + async (x: number, y: number, z: number): Promise => { + return new Promise(); + }; + const cccc = 1, + bbb = { + "x": 0 + }, + ddddd = 0; +}; +=-=-= + Code: (lambda () (setq indent-tabs-mode nil) -- 2.34.1