From daef546b1c28827be7de56207faf3cc41cd71127 Mon Sep 17 00:00:00 2001 From: Tama McGlinn Date: Thu, 23 Dec 2021 10:34:48 +0100 Subject: [PATCH] Add isort for python import sorting --- autoload/codefmt/isort.vim | 51 ++++++++++++++++++++++++++++++++++++++ doc/codefmt.txt | 4 +++ instant/flags.vim | 4 +++ plugin/register.vim | 1 + 4 files changed, 60 insertions(+) create mode 100644 autoload/codefmt/isort.vim diff --git a/autoload/codefmt/isort.vim b/autoload/codefmt/isort.vim new file mode 100644 index 0000000..40bf2f0 --- /dev/null +++ b/autoload/codefmt/isort.vim @@ -0,0 +1,51 @@ +" Copyright 2020 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: isort +function! codefmt#isort#GetFormatter() abort + let l:formatter = { + \ 'name': 'isort', + \ 'setup_instructions': 'Install isort ' . + \ '(https://pypi.python.org/pypi/isort/).'} + + function l:formatter.IsAvailable() abort + return executable(s:plugin.Flag('isort_executable')) + endfunction + + function l:formatter.AppliesToBuffer() abort + return &filetype is# 'python' + endfunction + + "" + " Reformat the current buffer with isort or the binary named in + " @flag(isort_executable) + " + " We implement Format(), and not FormatRange{,s}(), because isort doesn't + " provide a hook for formatting a range + function l:formatter.Format() abort + let l:executable = s:plugin.Flag('isort_executable') + + call codefmt#formatterhelpers#Format([ + \ l:executable, + \ '-']) + endfunction + + return l:formatter +endfunction diff --git a/doc/codefmt.txt b/doc/codefmt.txt index 4fa2324..d5f3fd5 100644 --- a/doc/codefmt.txt +++ b/doc/codefmt.txt @@ -93,6 +93,10 @@ Default: 'yapf' ` The path to the black executable. Default: 'black' ` + *codefmt:isort_executable* +The path to the isort executable. +Default: 'isort' ` + *codefmt:gn_executable* The path to the gn executable. Default: 'gn' ` diff --git a/instant/flags.vim b/instant/flags.vim index c414e7a..8694c31 100644 --- a/instant/flags.vim +++ b/instant/flags.vim @@ -88,6 +88,10 @@ call s:plugin.Flag('yapf_executable', 'yapf') " The path to the black executable. call s:plugin.Flag('black_executable', 'black') +"" +" The path to the isort executable. +call s:plugin.Flag('isort_executable', 'isort') + "" " The path to the gn executable. call s:plugin.Flag('gn_executable', 'gn') diff --git a/plugin/register.vim b/plugin/register.vim index 19fe7fe..603378d 100644 --- a/plugin/register.vim +++ b/plugin/register.vim @@ -70,6 +70,7 @@ call s:registry.AddExtension(codefmt#ktfmt#GetFormatter()) call s:registry.AddExtension(codefmt#luaformatterfiveone#GetFormatter()) call s:registry.AddExtension(codefmt#nixpkgs_fmt#GetFormatter()) call s:registry.AddExtension(codefmt#autopep8#GetFormatter()) +call s:registry.AddExtension(codefmt#isort#GetFormatter()) call s:registry.AddExtension(codefmt#black#GetFormatter()) call s:registry.AddExtension(codefmt#yapf#GetFormatter()) call s:registry.AddExtension(codefmt#rustfmt#GetFormatter())