Skip to content

Commit

Permalink
feat(fmt): Add formatters (#232)
Browse files Browse the repository at this point in the history
* feat(fmt): Add formatters

* chore: Changelog

* correct spelling

* don't test foramt elisp-autofmt
  • Loading branch information
jcs090218 committed Mar 5, 2024
1 parent c7f5d6f commit 3e8d89f
Show file tree
Hide file tree
Showing 16 changed files with 360 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
* feat: Add `loc` command (#227)
* fix: Allow initialize in other scopes (#229)
* feat: Rename command `check-eask` to `analyze` (#230)
* feat(fmt): Add formatters (#232)

## 0.9.x
> Released Nov 17, 2023
Expand Down
38 changes: 38 additions & 0 deletions cmds/core/format.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright (C) 2024 the Eask authors.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

"use strict";

exports.command = ['format <type>', 'fmt <type>'];
exports.desc = 'Run formatters';
exports.builder = function (yargs) {
yargs.usage(`${exports.desc}
Usage: eask format <type> [options..]`)
.commandDir('../format/')
.demandCommand();

/* XXX: Configure only in the menu. */
if (UTIL.cmd_count() == 1) {
yargs.positional(
'<type>', {
description: 'type of the formatter',
});
}
}

exports.handler = async (argv) => { };
2 changes: 1 addition & 1 deletion cmds/core/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"use strict";

exports.command = ['lint <type>'];
exports.desc = 'Run linter';
exports.desc = 'Run linters';
exports.builder = function (yargs) {
yargs.usage(`${exports.desc}
Expand Down
2 changes: 1 addition & 1 deletion cmds/core/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"use strict";

exports.command = ['test <type>'];
exports.desc = 'Run test';
exports.desc = "Run regression/unit tests";
exports.builder = function (yargs) {
yargs.usage(`${exports.desc}
Expand Down
31 changes: 31 additions & 0 deletions cmds/format/elfmt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright (C) 2024 the Eask authors.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

"use strict";

exports.command = ['elfmt [files..]'];
exports.desc = 'Reformat Elisp source with elfmt';
exports.builder = yargs => yargs
.positional(
'[files..]', {
description: 'specify files to do elfmt',
type: 'array',
});

exports.handler = async (argv) => {
await UTIL.e_call(argv, 'format/elfmt', argv.files);
};
31 changes: 31 additions & 0 deletions cmds/format/elisp-autofmt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright (C) 2024 the Eask authors.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

"use strict";

exports.command = ['elisp-autofmt [files..]'];
exports.desc = 'Reformat Elisp source with elisp-autofmt';
exports.builder = yargs => yargs
.positional(
'[files..]', {
description: 'specify files to do elisp-autofmt',
type: 'array',
});

exports.handler = async (argv) => {
await UTIL.e_call(argv, 'format/elisp-autofmt', argv.files);
};
2 changes: 1 addition & 1 deletion cmds/generate/autoloads.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2022-2023 the Eask authors.
* Copyright (C) 2022-2024 the Eask authors.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down
5 changes: 3 additions & 2 deletions docs/content/Getting-Started/Basic-Usage/_index.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ Commands:
path [patterns..] Print the PATH (exec-path) from workspace [aliases: exec-path]
exec [args..] Execute command with correct environment PATH set up
files [patterns..] Print all package files
format <type> Run formatters [aliases: fmt]
generate <type> Generate files that are used for the development
info Display information about the current package
init [files..] Initialize project to use Eask
install-deps Automatically install package dependencies [aliases: install-dependencies, prepare]
install [names..] Install packages
keywords List available keywords that can be used in the header section
link <action> Manage links
lint <type> Run linter
lint <type> Run linters
list List packages
load-path [patterns..] Print the load-path from workspace
load [files..] Load elisp files
Expand All @@ -63,7 +64,7 @@ Commands:
run <type> Run custom tasks
search [queries..] Search packages
status Display the state of the workspace
test <type> Run test
test <type> Run regression/unit tests
uninstall [names..] Uninstall packages [aliases: delete]
upgrade [names..] Upgrade packages
locate Print out Eask installed location
Expand Down
5 changes: 3 additions & 2 deletions docs/content/Getting-Started/Basic-Usage/_index.zh-tw.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ Commands:
path [patterns..] Print the PATH (exec-path) from workspace [aliases: exec-path]
exec [args..] Execute command with correct environment PATH set up
files [patterns..] Print all package files
format <type> Run formatters [aliases: fmt]
generate <type> Generate files that are used for the development
info Display information about the current package
init [files..] Initialize project to use Eask
install-deps Automatically install package dependencies [aliases: install-dependencies, prepare]
install [names..] Install packages
keywords List available keywords that can be used in the header section
link <action> Manage links
lint <type> Run linter
lint <type> Run linters
list List packages
load-path [patterns..] Print the load-path from workspace
load [files..] Load elisp files
Expand All @@ -60,7 +61,7 @@ Commands:
run <type> Run custom tasks
search [queries..] Search packages
status Display the state of the workspace
test <type> Run test
test <type> Run regression/unit tests
uninstall [names..] Uninstall packages [aliases: delete]
upgrade [names..] Upgrade packages
locate Print out Eask installed location
Expand Down
27 changes: 27 additions & 0 deletions docs/content/Getting-Started/Commands-and-options/_index.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,8 @@ $ eask [GLOBAL-OPTIONS] link list

# 🚩 Cleaning

Delete various files produced during building.

## 🔍 eask clean workspace

Delete `.eask` from the current workspace.
Expand Down Expand Up @@ -753,6 +755,8 @@ $ eask [GLOBAL-OPTIONS] lint regexps [FILES..]

# 🚩 Testing

Run regression/unit tests.

## 🔍 eask test activate

Activate package; use to test the package activation
Expand Down Expand Up @@ -803,6 +807,26 @@ $ eask [GLOBAL-OPTIONS] test melpazoid [DIRECTORIES..]
💡 If **[DIRECTORIES..]** is not passed in; it will use the current workspace instead.
{{< /hint >}}

# 🚩 Formatting

Commands that formats your Emacs source files.

## 🔍 eask format elisp-autofmt

Run [elisp-autofmt][] formatter.

```sh
$ eask [GLOBAL-OPTIONS] format elisp-autofmt [FILES..]
```

## 🔍 eask format elfmt

Run [elfmt][] formatter.

```sh
$ eask [GLOBAL-OPTIONS] format elfmt [FILES..]
```

# 🚩 Control DSL

List of commands that control DSL.
Expand Down Expand Up @@ -1040,3 +1064,6 @@ Do not use a proxy for any URL matching pattern.
[ert-runner]: https://github.com/rejeep/ert-runner.el
[buttercup]: https://github.com/jorgenschaefer/emacs-buttercup
[melpazoid]: https://github.com/riscy/melpazoid

[elisp-autofmt]: https://codeberg.org/ideasman42/emacs-elisp-autofmt
[elfmt]: https://github.com/riscy/elfmt
27 changes: 27 additions & 0 deletions docs/content/Getting-Started/Commands-and-options/_index.zh-tw.md
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,8 @@ $ eask [GLOBAL-OPTIONS] link list

# 🚩 清理

刪除建置過程中產生的各種檔案。

## 🔍 eask clean workspace

從當前工作區中刪除 `.eask`
Expand Down Expand Up @@ -740,6 +742,8 @@ $ eask [GLOBAL-OPTIONS] lint regexps [FILES..]

# 🚩 測試框架

運行回歸/單元測試。

## 🔍 eask test activate

激活包; 用於測試包激活
Expand Down Expand Up @@ -790,6 +794,26 @@ $ eask [GLOBAL-OPTIONS] test melpazoid [DIRECTORIES..]
💡 如果未傳入 **[DIRECTORIES..]**,它將使用目前工作空間。
{{< /hint >}}

# 🚩 格式化

格式化 Emacs 源文件的命令。

## 🔍 eask format elisp-autofmt

運行 [elisp-autofmt][] 格式器.

```sh
$ eask [GLOBAL-OPTIONS] format elisp-autofmt [FILES..]
```

## 🔍 eask format elfmt

運行 [elfmt][] 格式器.

```sh
$ eask [GLOBAL-OPTIONS] format elfmt [FILES..]
```

# 🚩 控制 DSL

控制 DSL 的指令列表。
Expand Down Expand Up @@ -1026,3 +1050,6 @@ $ eask --proxy "localhost:8888" [COMMAND]
[ert-runner]: https://github.com/rejeep/ert-runner.el
[buttercup]: https://github.com/jorgenschaefer/emacs-buttercup
[melpazoid]: https://github.com/riscy/melpazoid

[elisp-autofmt]: https://codeberg.org/ideasman42/emacs-elisp-autofmt
[elfmt]: https://github.com/riscy/elfmt
79 changes: 79 additions & 0 deletions lisp/format/elfmt.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
;;; format/elfmt.el --- Run elfmt -*- lexical-binding: t; -*-

;;; Commentary:
;;
;; Commmand use to run `elfmt' for all files
;;
;; $ eask format elfmt [files..]
;;
;;
;; Positionals:
;;
;; [files..] files you want elfmt to run on
;;

;;; Code:

(let ((dir (file-name-directory (nth 1 (member "-scriptload" command-line-args)))))
(load (expand-file-name "_prepare.el"
(locate-dominating-file dir "_prepare.el"))
nil t))

;;
;;; Externals

(declare-function elfmt-buffer "ext:elisp-autofmt.el")

;;
;;; Flags

(advice-add #'eask-allow-error-p :override #'always)

;;
;;; Core

(defconst eask-format-elfmt--version nil
"`elfmt' version.")

(defun eask-format-elfmt--file (filename)
"Run elfmt on FILENAME."
(let* ((filename (expand-file-name filename))
(file (eask-root-del filename)))
(with-current-buffer (find-file filename)
(elfmt-buffer)
(save-buffer)
(kill-buffer))))

(eask-start
;; Preparation
(eask-with-archives '("gnu" "melpa" "jcs-elpa")
(eask-package-install 'elfmt))
(setq eask-format-elfmt--version (eask-package--version-string 'elfmt))

;; Start formatting
(require 'elfmt)
(let* ((patterns (eask-args))
(files (if patterns
(eask-expand-file-specs patterns)
(eask-package-el-files))))
(cond
;; Files found, do the action!
(files
(eask-msg "")
(eask-msg "Running `%s` formatter (%s)"
(ansi-green "elfmt") (ansi-yellow eask-format-elfmt--version))
(eask-progress-seq " - Formatting" files "done! ✓" #'eask-format-elfmt--file)
(eask-msg "")
(eask-info "(Total of %s file%s %s formatted)" (length files)
(eask--sinr files "" "s")
(eask--sinr files "has" "have")))
;; Pattern defined, but no file found!
(patterns
(eask-info "(No files match wildcard: %s)"
(mapconcat #'identity patterns " ")))
;; Default, print help!
(t
(eask-info "(No files have been formatted)")
(eask-help "format/elfmt")))))

;;; format/elfmt.el ends here
Loading

0 comments on commit 3e8d89f

Please sign in to comment.