Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go.mod file support #1931

Merged
merged 2 commits into from
Aug 31, 2018
Merged

go.mod file support #1931

merged 2 commits into from
Aug 31, 2018

Conversation

fatih
Copy link
Owner

@fatih fatih commented Aug 30, 2018

This adds initial support for the go.mod file. It adds the
followings:

  • Syntax highligthing. We highlight keywords, strings, operator and
    semver version. It works pretty well for now.
  • Auto fmt on save. Command :GoModFmt or Plug(go-mod-fmt) for custom
    mappings

Before we fully support the semantics of go.mod, I think this initially
will be helpful because I discovered that go.mod is read and edited a
lot. So going forward, this will make it easier experimenting with Go
modules.

  • Add documentation
  • Make sure auto modfmt toggle commands work fine
  • Add v1.11 guard for :GoModFmt

related: #1906

@fatih fatih added the wip label Aug 30, 2018
@fatih fatih mentioned this pull request Aug 30, 2018
4 tasks
Copy link
Collaborator

@bhcleek bhcleek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work @fatih. It's exciting to see this support being added. I've left a few comments; most of them are either about possibly mistakenly added files or trying to be consistent with other approaches around error and list handling.

@@ -0,0 +1 @@
/Users/fatih/go/src/github.com/fatih/goautofix/vim/fix.vim
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it was mistakenly committed.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it was symlinked (I was lazy) from a personal project for a small tool I wrote and wanted to try out. Removed them :)

call go#list#Window(l:listtype, len(a:errors))
endfunction

function! go#mod#ToggleModfmtAutoSave() abort
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fmt should probably be Fmt for consistency...

let l:listtype = go#list#Type("GoModFmt")
if !empty(a:errors)
call go#list#Populate(l:listtype, a:errors, 'Format')
echohl Error | echomsg "GoModFmt returned error" | echohl None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use go#util#EchoError instead...

function! s:show_errors(errors) abort
let l:listtype = go#list#Type("GoModFmt")
if !empty(a:errors)
call go#list#Populate(l:listtype, a:errors, 'Format')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of passing in the parsed errors and calling go#list#Populate manually, can you use an errorformat string and call go#list#ParseFormat?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tried it, but I think it'll not work because we run the command on a temporary file first (because go mod edit does an in-place edit). This means that the initial %f is the temporary filename. This is copied from fmt.vim and it's the same issue. I'll check if I can do something ( I would prefer ParseFormat indeed)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmmm... If you know the temporary filename, then it should be easy enough to do a substitute(...) on a:errors before passing to ParseFormat.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think at this point, this is too much a hack. We're having a working version and it really works fine. Not sure if it's worth doing it (same applies for gofmt).

" list of errors to be put into location list
let errors = []
for line in splitted
let tokens = matchlist(line, '^\(.\{-}\):\(\d\+\):\(\d\+\)\s*\(.*\)')
Copy link
Collaborator

@bhcleek bhcleek Aug 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This regex is relatively easy to turn into an errorformat string. It looks like it follows the usual pattern, so the errorformat string would be %f:%l:%c:\ %m

@@ -0,0 +1 @@
/Users/fatih/go/src/github.com/fatih/goautofix/vim/commands-fix.vim
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this another mistakenly added file?

@@ -31,4 +31,8 @@ au BufReadPost *.s call s:gofiletype_post()

au BufRead,BufNewFile *.tmpl set filetype=gohtmltmpl

au BufNewFile *.mod setfiletype gomod | setlocal fileencoding=utf-8 fileformat=unix
au BufRead *.mod call s:gofiletype_pre("gomod")
au BufReadPost *.mod call s:gofiletype_post()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This conflicts with a built-in filetype: https://github.com/vim/vim/blob/master/runtime/filetype.vim#L996-L1002

Would probably be better to explicitly use go.mod, and maybe even check if the first line matches module <ImportPath>?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch @Carpetsmoker !

go.mod seems like the right call to me, maybe in conjunction with check the first line?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add the checking line 👍 However, can I really use a . inside a ft? I checked the link martin gave and a filetype is always a single word. I think we keep it gomod. It's not that bad.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind. I misunderstood, gonna use explicit go.mod :)

This adds initial support for the `go.mod` file.  It adds the
followings:

* Syntax highligthing. We highlight keywords, strings, operator and
semver version. It works pretty well for now.
* Auto fmt on save. Command `:GoModFmt` or `Plug(go-mod-fmt)` for custom
  mappings

Before we fully support the semantics of go.mod, I think this initially
will be helpful because I discovered that `go.mod` is read and edited a
lot. So going forward, this will make it easier experimenting with Go
modules.

related: #1906
@fatih
Copy link
Owner Author

fatih commented Aug 30, 2018

@bhcleek @Carpetsmoker fixed all review comments and added a couple of improvements. PTAL

@bhcleek
Copy link
Collaborator

bhcleek commented Aug 30, 2018

lgtm, but there's linting problems.

@voutasaurus
Copy link

lgtm, but there's linting problems.
https://github.com/fatih/vim-go/pull/1931/files#diff-d7201d5be221f16a9b1825e6a48685e9R1905

It's just one line in the docs that's over the line limit (needs to be wrapped/rewritten). I'd fix it if you want (I don't have access though).

* Fix auto toggle command
* Add guard agains Go versions lower than 1.11
* Add documentation
* Fixed setting correct filetype
* Fixed parsing errors
* Changed variable names to be consistent
@fatih
Copy link
Owner Author

fatih commented Aug 31, 2018

@bhcleek @voutasaurus thanks! It was late after I pushed it, so didn't check the Travis results. I fixed and force pushed it. I'll merge it once it's green.

@fatih fatih merged commit c1dd611 into master Aug 31, 2018
@fatih fatih deleted the go-modules-syntax branch August 31, 2018 05:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants