Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
66 changes: 66 additions & 0 deletions autoload/codefmt/ormolu.vim
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions instant/flags.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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')
2 changes: 2 additions & 0 deletions plugin/register.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
131 changes: 131 additions & 0 deletions vroom/ormolu.vroom
Original file line number Diff line number Diff line change
@@ -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(...)<CR>
| call add(g:repeat_calls, a:000)<CR>
: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 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
! myormolu .*
$ f()
:Glaive codefmt ormolu_executable='ormolu'

You can format any buffer with ormolu specifying the formatter explicitly.

@clear
% module Main<CR>
|<CR>
| where<CR>
|<CR>
|main:: IO ()<CR>
|<CR>
|<CR>
|{- this is just a comment -}<CR>
|main =pure ()<CR>
|<CR>
|foo :: Int<CR>
|foo =<CR>
| 5<CR>
| + 5<CR>
|<CR>
|bar :: Int<CR>
| 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<CR>
|<CR>
|main :: IO ()<CR>
|{- this is just a comment -}<CR>
|main = pure ()<CR>
|<CR>
|foo :: Int<CR>
|foo =<CR>
| 5<CR>
| + 5<CR>
|<CR>
|bar :: Int<CR>
| 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