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 (bug#68054).
This commit is contained in:
Noah Peart 2024-04-19 01:46:50 -07:00 committed by Dmitry Gutov
parent a486782f5e
commit ce5d004b5b
2 changed files with 44 additions and 1 deletions

View file

@ -91,6 +91,17 @@ Check if a node type is available, then return the right indent rules."
`(((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,8 @@ Argument LANGUAGE is either `typescript' or `tsx'."
((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 ,(rx (or "variable" "lexical") "_" (or "declaration" "declarator")))
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)

View file

@ -62,6 +62,37 @@ 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 cccc = 1,
bbb = {
"x": 0
},
ddddd = 0;
// First decls with value starting on same line
const a = (x: string): string => {
return x + x;
};
var bbb = {
"x": 0
};
};
=-=-=
Code:
(lambda ()
(setq indent-tabs-mode nil)