Skip to content

Instantly share code, notes, and snippets.

@Eoksni
Created May 12, 2017 14:12
Show Gist options
  • Save Eoksni/96f30d0bef01ca1acb63a42f22553b93 to your computer and use it in GitHub Desktop.
Save Eoksni/96f30d0bef01ca1acb63a42f22553b93 to your computer and use it in GitHub Desktop.
Quick hack to populate tide-format-options using .vscode/settings.json for formatting
((nil . ((eval . (progn
(make-local-variable 'tide-format-options)
(setq tide-format-options (dmaz-parse-vscode-formatting (dmaz-joindirs (dmaz-find-dirlocals-dir) ".vscode" "settings.json"))))))))
(defun dmaz-joindirs (root &rest dirs)
"Joins a series of directories together, like Python's os.path.join,
(dmaz-joindirs \"/tmp\" \"a\" \"b\" \"c\") => /tmp/a/b/c"
(if (not dirs)
root
(apply 'dmaz-joindirs
(expand-file-name (car dirs) root)
(cdr dirs))))
(defun dmaz-find-dirlocals-dir ()
"Finds directory where closest .dir-locals file is located"
(file-name-directory
(let ((d (dir-locals-find-file ".")))
(if (stringp d) d (car d)))))
(defun dmaz-parse-vscode-formatting (f)
"Parses `f' as .vscode/settings.json to get formatting options suitable for `tide-format-options',
(setq tide-format-options (dmaz-parse-vscode-formatting \".vscode/settings.json\"))"
(require 'cl)
(let* ((file-contents (with-temp-buffer
(insert-file-contents f)
(buffer-substring-no-properties
(point-min)
(point-max))))
(string-list (split-string file-contents "\n" t))
(filtered-list (remove-if-not #'(lambda (s)
(string-match ".*\"javascript\\.format\\.\\(.*\\)\": \\([a-z]*\\)" s))
string-list))
(values-list (mapcan #'(lambda (s)
(string-match ".*\"javascript\\.format\\.\\(.*\\)\": \\([a-z]*\\)" s)
(let ((name (match-string 1 s))
(value (if (equal (match-string 2 s) "true") t nil)))
`(,(intern (concat ":" name)) ,value))
) filtered-list)))
values-list))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment