branch: elpa/typescript-mode
commit 2815dd1b4bbf902fb61e0a8f1aca6319c7b0c940
Author: jack-williams <[email protected]>
Commit: Louis-Dominique Dubeau <[email protected]>
Add option to support indented case-statements. Fixes #23 (#66)
Add the `typescript-indent-switch-clauses` customization option. At the
default of `t`, it causes the mode to follow tsserver's indentation. When set
to `nil`, it causes the mode to follow the indentation that `typescript-mode`
was originally offering.
---
test-files/indentation-reference-document.ts | 23 ++++---------------
test-files/switch-case-indent-default.ts | 30 ++++++++++++++++++++++++
test-files/switch-case-indent-disabled.ts | 30 ++++++++++++++++++++++++
typescript-mode-tests.el | 24 +++++++++++++++++++-
typescript-mode.el | 34 ++++++++++++++++++++--------
5 files changed, 112 insertions(+), 29 deletions(-)
diff --git a/test-files/indentation-reference-document.ts
b/test-files/indentation-reference-document.ts
index 7ba8328968..fe06883a14 100644
--- a/test-files/indentation-reference-document.ts
+++ b/test-files/indentation-reference-document.ts
@@ -21,25 +21,10 @@ namespace ts.server {
// functions indent.
function getGlobalTypingsCacheLocation() {
- // We know switch/case is indented incorrectly.
- // TODO: FIX!
-
- // switch (process.platform) {
- // case "win32": {
- // const basePath = process.env.LOCALAPPDATA ||
- // process.env.APPDATA;
- // return combinePaths(normalizeSlashes(basePath),
"Microsoft/TypeScript");
- // }
- // case "darwin":
- // case "linux":
- // case "android": {
- // const cacheLocation =
getNonWindowsCacheLocation(process.platform === "darwin");
- // return combinePaths(cacheLocation, "typescript");
- // }
- // default:
- // Debug.fail(`unsupported platform '${process.platform}'`);
- // return;
- // }
+ const obj = {
+ case: 1,
+ default: 2
+ };
}
// interfaces and classes indent.
diff --git a/test-files/switch-case-indent-default.ts
b/test-files/switch-case-indent-default.ts
new file mode 100644
index 0000000000..b1af8804b5
--- /dev/null
+++ b/test-files/switch-case-indent-default.ts
@@ -0,0 +1,30 @@
+function indentTest(): any {
+ const obj = {
+ case: 1,
+ default: 2
+ };
+ switch (process.platform) {
+ case "win32": {
+ const basePath = process.env.LOCALAPPDATA ||
+ process.env.APPDATA;
+ return combinePaths(normalizeSlashes(basePath),
"Microsoft/TypeScript");
+ }
+ case "darwin":
+ const objCase = {
+ case: 1,
+ default: 2
+ };
+ case "linux":
+ case "android": {
+ const cacheLocation = getNonWindowsCacheLocation(process.platform
=== "darwin");
+ return combinePaths(cacheLocation, "typescript");
+ }
+ default:
+ const objDefault = {
+ case: 1,
+ default: 2
+ };
+ Debug.fail(`unsupported platform '${process.platform}'`);
+ return;
+ }
+}
diff --git a/test-files/switch-case-indent-disabled.ts
b/test-files/switch-case-indent-disabled.ts
new file mode 100644
index 0000000000..3b29dba743
--- /dev/null
+++ b/test-files/switch-case-indent-disabled.ts
@@ -0,0 +1,30 @@
+function getGlobalTypingsCacheLocation() {
+ const obj = {
+ case: 1,
+ default: 2
+ };
+ switch (process.platform) {
+ case "win32": {
+ const basePath = process.env.LOCALAPPDATA ||
+ process.env.APPDATA;
+ return combinePaths(normalizeSlashes(basePath),
"Microsoft/TypeScript");
+ }
+ case "darwin":
+ const objCase = {
+ case: 1,
+ default: 2
+ };
+ case "linux":
+ case "android": {
+ const cacheLocation = getNonWindowsCacheLocation(process.platform ===
"darwin");
+ return combinePaths(cacheLocation, "typescript");
+ }
+ default:
+ const objDefault = {
+ case: 1,
+ default: 2
+ };
+ Debug.fail(`unsupported platform '${process.platform}'`);
+ return;
+ }
+}
diff --git a/typescript-mode-tests.el b/typescript-mode-tests.el
index da8f9a3aa8..487dc2fa8d 100644
--- a/typescript-mode-tests.el
+++ b/typescript-mode-tests.el
@@ -18,15 +18,37 @@
(untabify (point-min) (point-max)))
(ert-deftest indentation-reference-document-is-reflowed-correctly ()
- (let* ((buffer (find-file "test-files/indentation-reference-document.ts")))
+ (let ((buffer (find-file "test-files/indentation-reference-document.ts")))
;; double ensure mode is active
(typescript-mode)
+ (let ((test-reference (typescript-test-get-doc)))
+ (typescript-test-indent-all)
+ (should (string-equal test-reference
+ (typescript-test-get-doc)))
+ (let ((typescript-indent-switch-clauses nil))
+ (typescript-test-indent-all)
+ (should (string-equal test-reference
+ (typescript-test-get-doc)))))
+ (kill-buffer buffer)))
+
+(ert-deftest switch-case-indent-default ()
+ (let ((buffer (find-file "test-files/switch-case-indent-default.ts")))
+ (typescript-mode)
(let ((test-reference (typescript-test-get-doc)))
(typescript-test-indent-all)
(should (string-equal test-reference
(typescript-test-get-doc))))
+ (kill-buffer buffer)))
+(ert-deftest switch-case-indent-disabled ()
+ (let ((buffer (find-file "test-files/switch-case-indent-disabled.ts"))
+ (typescript-indent-switch-clauses nil))
+ (typescript-mode)
+ (let ((test-reference (typescript-test-get-doc)))
+ (typescript-test-indent-all)
+ (should (string-equal test-reference
+ (typescript-test-get-doc))))
(kill-buffer buffer)))
(defun get-all-matched-strings (to-match)
diff --git a/typescript-mode.el b/typescript-mode.el
index 16e3f1f890..bd2f332e4a 100644
--- a/typescript-mode.el
+++ b/typescript-mode.el
@@ -591,6 +591,13 @@ The value must be no less than minus
`typescript-indent-level'."
:safe 'integerp
:group 'typescript)
+(defcustom typescript-indent-switch-clauses t
+ "Enable indenting of switch case and default clauses to
+replicate tsserver behaviour. Indent level is taken to be
+`typescript-indent-level'."
+ :type 'boolean
+ :group 'typescript)
+
(defcustom typescript-auto-indent-flag t
"Whether to automatically indent when typing punctuation characters.
If non-nil, the characters {}();,: also indent the current line
@@ -2203,8 +2210,8 @@ moved on success."
((eq (char-after) ?#) 0)
((save-excursion (typescript--beginning-of-macro)) 4)
((nth 1 parse-status)
- (let ((same-indent-p (looking-at
- "[]})]\\|\\_<case\\_>\\|\\_<default\\_>"))
+ (let ((same-indent-p (looking-at "[]})]"))
+ (switch-keyword-p (looking-at
"\\_<default\\_>\\|\\_<case\\_>[^:]"))
(continued-expr-p (typescript--continued-expression-p)))
(goto-char (nth 1 parse-status))
(if (looking-at "[({[]\\s-*\\(/[/*]\\|$\\)")
@@ -2214,13 +2221,22 @@ moved on success."
(eq (char-before) ?\)))
(backward-list))
(back-to-indentation)
- (cond (same-indent-p
- (current-column))
- (continued-expr-p
- (+ (current-column) (* 2 typescript-indent-level)
- typescript-expr-indent-offset))
- (t
- (+ (current-column) typescript-indent-level))))
+ (let* ((in-switch-p (unless same-indent-p
+ (looking-at "\\_<switch\\_>")))
+ (same-indent-p (or same-indent-p
+ (and switch-keyword-p
+ in-switch-p)))
+ (indent
+ (cond (same-indent-p
+ (current-column))
+ (continued-expr-p
+ (+ (current-column) (* 2
typescript-indent-level)
+ typescript-expr-indent-offset))
+ (t
+ (+ (current-column)
typescript-indent-level)))))
+ (if (and in-switch-p typescript-indent-switch-clauses)
+ (+ indent typescript-indent-level)
+ indent)))
(unless same-indent-p
(forward-char)
(skip-chars-forward " \t"))