chapel-lang / chapel

a Productive Parallel Programming Language
https://chapel-lang.org
Other
1.76k stars 414 forks source link

Chapel emacs mode improvements #16224

Open bradcray opened 3 years ago

bradcray commented 3 years ago

Our Chapel emacs mode is better than nothing, but leaves a lot to be desired. This issue is designed to capture the desire for a new/improved emacs mode, whether by improving the current mode or starting from scratch (e.g., see https://github.com/damon-kwok/chapel-mode for a recent effort).

List of issues with the current mode (will add more as they come up):

if loc == here {
                writeln("Hi");
}
if test then
  foo();
 else
   bar();

(there are definitely other more major ones, but I can't reproduce them all at the moment... will append to this issue as I come back across them, which is frequently)

damon-kwok commented 3 years ago

@bradcray It's easy.

(defconst chapel-indent-start-keywords
  '("class" "record" "module"  
    "if" "then" "else" "for" ...... )
  "Chapel keywords which indicate a new indentation level.")

(defun chapel--looking-at-indent-start ()
  "Determines if the current position is 'looking at' a keyword that start new indentation."
  (-any? (lambda (k)
           (looking-at (concat  "^[ \t]*" k "\\($\\|[ \t]\\)"))) chapel-indent-start-keywords))
(defun chapel-indent-line ()
  ......
  ((chapel--looking-at-indent-start)
     (setq cur-indent (+ (current-indentation) tab-width)))
  ......)
(define-derived-mode chapel-mode prog-mode
  "Chapel"
  "Major mode for editing chapel files."
  ......
  (setq-local indent-tabs-mode nil)
  (setq-local tab-width 2)
  (setq-local buffer-file-coding-system 'utf-8-unix)
  (setq-local indent-line-function 'chapel-indent-line)
  ......)
damon-kwok commented 3 years ago

If chapel can provide a fmt subcommand to format the chpl file, I can add an automatic formatting hook.

damon-kwok commented 3 years ago

@bradcray I chose a simpler way, adding the missing then keyword to the indentation of js.

(defconst chapel-indent-keywords
  '("catch" "do" "else" "finally" "for" "if" "then" "try" "while" "with" "each")
  "Chapel keywords which indicate a new indentation level.")

(define-derived-mode chapel-mode prog-mode
  "Chapel"
  "Major mode for editing Chapel files."
  :syntax-table chapel-mode-syntax-table
  ;;
  (setq-local indent-tabs-mode nil)
  (setq-local tab-width 2)
  (setq-local buffer-file-coding-system 'utf-8-unix)
  ;;
  (setq-local electric-indent-chars (append "{}():;," electric-indent-chars))
  (setq-local js-indent-level tab-width)
  (setq-local js--possibly-braceless-keyword-re ;;
    (js--regexp-opt-symbol chapel-indent-keywords))
  (setq-local indent-line-function #'js-indent-line)
  ......)

When I first saw the church syntax, I thought it was closer to js than c. Moreover, C's highlight strategy and indentation rules are clearly outdated. If lsp-server is not used, the performance of C language is just as bad.

At present, I don't know enough about chapel. I hope that the new chapel-mode will improve the experience of chapel programmers in the future.

damon-kwok commented 3 years ago

@bradcray I have good news, now chapel-mode can automatically format the code.