From 435a98f44602f6a2dbddab347d8f22ed6ee3a185 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dimitrije=20Radojevi=C4=87?= Date: Wed, 15 Dec 2021 10:33:47 +0100 Subject: [PATCH 1/5] Add ormolu formatter (Haskell). --- autoload/codefmt/ormolu.vim | 66 +++++++++++++++++++++++++++++++++++++ instant/flags.vim | 4 +++ plugin/register.vim | 2 ++ 3 files changed, 72 insertions(+) create mode 100644 autoload/codefmt/ormolu.vim diff --git a/autoload/codefmt/ormolu.vim b/autoload/codefmt/ormolu.vim new file mode 100644 index 0000000..dcdb424 --- /dev/null +++ b/autoload/codefmt/ormolu.vim @@ -0,0 +1,66 @@ +" Copyright 2021 Google Inc. All rights reserved. +" +" Licensed under the Apache License, Version 2.0 (the "License"); +" you may not use this file except in compliance with the License. +" You may obtain a copy of the License at +" +" http://www.apache.org/licenses/LICENSE-2.0 +" +" Unless required by applicable law or agreed to in writing, software +" distributed under the License is distributed on an "AS IS" BASIS, +" WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +" See the License for the specific language governing permissions and +" limitations under the License. + + +let s:plugin = maktaba#plugin#Get('codefmt') + + +"" +" @private +" Formatter: ormolu +function! codefmt#ormolu#GetFormatter() abort + let l:formatter = { + \ 'name': 'ormolu', + \ 'setup_instructions': 'Install ormolu ' . + \ '(https://hackage.haskell.org/package/ormolu).'} + + function l:formatter.IsAvailable() abort + return executable(s:plugin.Flag('ormolu_executable')) + endfunction + + function l:formatter.AppliesToBuffer() abort + return &filetype is# 'haskell' + endfunction + + "" + " Reformat the current buffer with ormolu or the binary named in + " @flag(ormolu_executable), only targeting the range between {startline} and + " {endline}. + " @throws ShellError + function l:formatter.FormatRange(startline, endline) abort + let l:cmd = [s:plugin.Flag('ormolu_executable')] + + let l:lines = getline(1, line('$')) + let l:input = join(l:lines, "\n") + + call maktaba#ensure#IsNumber(a:startline) + call maktaba#ensure#IsNumber(a:endline) + + if a:startline > 1 + call extend(l:cmd, ['--start-line', string(a:startline)]) + endif + call extend(l:cmd, ['--end-line', string(a:endline)]) + + try + let l:syscall = maktaba#syscall#Create(l:cmd).WithStdin(l:input) + let l:result = l:syscall.Call() + let l:formatted = split(l:result.stdout, "\n") + call maktaba#buffer#Overwrite(1, line('$'), l:formatted) + catch /ERROR(ShellError):/ + call maktaba#error#Shout('Error formatting file: %s', v:exception) + endtry + endfunction + + return l:formatter +endfunction diff --git a/instant/flags.vim b/instant/flags.vim index 900f673..f3c55de 100644 --- a/instant/flags.vim +++ b/instant/flags.vim @@ -227,3 +227,7 @@ call s:plugin.Flag('luaformatterfiveone_executable', 'luaformatterfiveone') "" " The path to the cljstyle executable. call s:plugin.Flag('cljstyle_executable', 'cljstyle') +" +"" +" The path to the ormolu executable. +call s:plugin.Flag('ormolu_executable', 'ormolu') diff --git a/plugin/register.vim b/plugin/register.vim index d783dd3..0c64a73 100644 --- a/plugin/register.vim +++ b/plugin/register.vim @@ -33,6 +33,7 @@ " * fish: fish_indent " * gn: gn " * go: gofmt +" * haskell: ormolu " * java: google-java-format " * javascript, json, html, css: js-beautify " * javascript, html, css, markdown: prettier @@ -77,3 +78,4 @@ call s:registry.AddExtension(codefmt#yapf#GetFormatter()) call s:registry.AddExtension(codefmt#rustfmt#GetFormatter()) call s:registry.AddExtension(codefmt#shfmt#GetFormatter()) call s:registry.AddExtension(codefmt#swiftformat#GetFormatter()) +call s:registry.AddExtension(codefmt#ormolu#GetFormatter()) From 1e9c48c2be0a4e03669c8d183151bb6c78e969bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dimitrije=20Radojevi=C4=87?= Date: Sun, 13 Nov 2022 11:55:41 +0100 Subject: [PATCH 2/5] Add vroom test for ormolu formatter. --- vroom/ormolu.vroom | 131 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 vroom/ormolu.vroom diff --git a/vroom/ormolu.vroom b/vroom/ormolu.vroom new file mode 100644 index 0000000..8225de3 --- /dev/null +++ b/vroom/ormolu.vroom @@ -0,0 +1,131 @@ +The ormolu formatter knows how to format Haskell. +If you aren't familiar with basic codefmt usage yet, see main.vroom first. + +We'll set up codefmt and configure the vroom environment, then jump into some +examples. + + :source $VROOMDIR/setupvroom.vim + + :let g:repeat_calls = [] + :function FakeRepeat(...) + | call add(g:repeat_calls, a:000) + :endfunction + :call maktaba#test#Override('repeat#set', 'FakeRepeat') + + :call codefmt#SetWhetherToPerformIsAvailableChecksForTesting(0) + + +The ormolu formatter expects the ormolu executable to be installed +on your system. + + % f() + :FormatCode ormolu + ! ormolu .* + $ f() + +The name or path of the gofmt executable can be configured via the +gofmt_executable flag if the default of "gofmt" doesn't work. + + :Glaive codefmt ormolu_executable='myormolu' + :FormatCode ormolu + ! myormolu .* + $ f() + :Glaive codefmt ormolu_executable='ormolu' + +You can format any buffer with ormolu specifying the formatter explicitly. + + @clear + % module Main + | + | where + | + |main:: IO () + | + | + |{- this is just a comment -} + |main =pure () + | + |foo :: Int + |foo = + | 5 + | + 5 + | + |bar :: Int + | bar = 7 + + :FormatCode ormolu + ! ormolu .*2>.* + $ module Main where + $ + $ main :: IO () + $ {- this is just a comment -} + $ main = pure () + $ + $ foo :: Int + $ foo = + $ 5 + $ + 5 + $ + $ bar :: Int + $ bar = 7 + module Main where + + main :: IO () + {- this is just a comment -} + main = pure () + + foo :: Int + foo = + 5 + + 5 + + bar :: Int + bar = 7 + @end + +It can format specific line ranges of code using :FormatLines. + + @clear + % module Main (main) where + | + |main :: IO () + |{- this is just a comment -} + |main = pure () + | + |foo :: Int + |foo = + | 5 + | + 5 + | + |bar :: Int + | bar = 7 + + :1,5FormatLines ormolu + ! ormolu .*2>.* + $ module Main (main) where + $ + $ main :: IO () + $ {- this is just a comment -} + $ main = pure () + $ + $ foo :: Int + $ foo = + $ 5 + $ + 5 + $ + $ bar :: Int + $ bar = 7 + module Main (main) where + + main :: IO () + {- this is just a comment -} + main = pure () + + foo :: Int + foo = + 5 + + 5 + + bar :: Int + bar = 7 + @end From 8c467505d4fd8bae744c69e1fa6111a0eefbbfba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dimitrije=20Radojevi=C4=87?= Date: Sun, 13 Nov 2022 12:10:59 +0100 Subject: [PATCH 3/5] List Haskell formatter in the README file. --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b42c4ab..7bd2938 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ helpfiles in the `doc/` directory. The helpfiles are also available via * Fish ([fish_indent](https://fishshell.com/docs/current/commands.html#fish_indent)) * Go (gofmt) * [GN](https://www.chromium.org/developers/gn-build-configuration) (gn) +* Haskell ([ormolu](https://github.com/tweag/ormolu)) * HTML (js-beautify) * Java (google-java-format or clang-format) * JavaScript (clang-format or [prettier](https://prettier.io)) From e8cbcb27e79302f2bf3490aed54db913058089dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dimitrije=20Radojevi=C4=87?= Date: Sun, 13 Nov 2022 12:12:19 +0100 Subject: [PATCH 4/5] Un-comment newline in flags. --- instant/flags.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/instant/flags.vim b/instant/flags.vim index f3c55de..b63c2d0 100644 --- a/instant/flags.vim +++ b/instant/flags.vim @@ -227,7 +227,7 @@ call s:plugin.Flag('luaformatterfiveone_executable', 'luaformatterfiveone') "" " The path to the cljstyle executable. call s:plugin.Flag('cljstyle_executable', 'cljstyle') -" + "" " The path to the ormolu executable. call s:plugin.Flag('ormolu_executable', 'ormolu') From 2777de952023dcf8d018f54566f493667f2a5322 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dimitrije=20Radojevi=C4=87?= Date: Sun, 13 Nov 2022 18:57:34 +0100 Subject: [PATCH 5/5] Oops, copy pasta leftovers. --- vroom/ormolu.vroom | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vroom/ormolu.vroom b/vroom/ormolu.vroom index 8225de3..954ad02 100644 --- a/vroom/ormolu.vroom +++ b/vroom/ormolu.vroom @@ -23,8 +23,8 @@ on your system. ! ormolu .* $ f() -The name or path of the gofmt executable can be configured via the -gofmt_executable flag if the default of "gofmt" doesn't work. +The name or path of the ormolu executable can be configured via the +ormolu_executable flag if the default of "ormolu" doesn't work. :Glaive codefmt ormolu_executable='myormolu' :FormatCode ormolu