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

Is there a way to get syntax checker working outside of a package? #143

Open
sdondley opened this issue Feb 21, 2020 · 12 comments
Open

Is there a way to get syntax checker working outside of a package? #143

sdondley opened this issue Feb 21, 2020 · 12 comments

Comments

@sdondley
Copy link

I'm new to swift so I'm not familiar with how things are supposed to work. Is there a way to get syntax errors to show up when editing a standalone file?

Similarly, if I edit a file that's part of an xcode project, is there a way to get syntax errors to display?

So far, I've only gotten this to work after init'ing a package and then editing Source/package/main.swift

@keith
Copy link
Owner

keith commented Feb 21, 2020

Definitely possible, the syntax checkers checked in to this repo currently only support swiftpm since that's such a common case, but neomake has a direct swiftc example that just parses the current file:

https://github.com/neomake/neomake/blob/76e5b2bad8e98e805ff4749068e6be6206bec2a0/autoload/neomake/makers/ft/swift.vim#L44-L54

@keith
Copy link
Owner

keith commented Feb 21, 2020

Kinda related: #142

@sdondley
Copy link
Author

Thanks. But I'm confused. I don't understand how your module works with Syntastic or how I would integrate neomake with your module or what swiftpm does exactly (I know what a package manager is but I have no idea why it's involved in syntax checking).

So what do I have to do, exactly, to get syntax checking on a standalone file working?

@sdondley
Copy link
Author

sdondley commented Feb 21, 2020

Also, another point of confusion for me is that I've got swift syntax checking working on Ubuntu. But when I run swift pacakage --version on my mac, I get this error:

error: unable to invoke subcommand: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-pacakage (No such file or directory)

So I'm not sure how to get this working on my Mac. I thought the package manager would be installed with xcode, though.

@keith
Copy link
Owner

keith commented Feb 21, 2020

You have a typo, it's swift package --version

@keith
Copy link
Owner

keith commented Feb 21, 2020

You have a few options:

  1. You can use this plugin and neomake, this plugin will provide highlighting while neomake would provide the syntax checking
  2. If you prefer your current syntax checker you can create a new checker in this repo similar to https://github.com/keith/swift.vim/blob/master/syntax_checkers/swift/swiftpm.vim but replace the fact that it's calling swift build and call swiftc -parse instead.

@sdondley
Copy link
Author

doh! thanks. I should put my glasses on, I guess. :)

@keith
Copy link
Owner

keith commented Feb 21, 2020

Note that SwiftPM isn't actually only doing syntax checking, the way it is setup in this repo it actually does a build of your package, and reports the compiler errors back

@sdondley
Copy link
Author

OK, I think this is starting to make more sense now. Thank you.

@sdondley
Copy link
Author

I took a stab at trying to create a new syntax checker (though my vimscript skills are about on par with my swift skills) called swiftc.vim:

if exists('g:loaded_syntastic_swift_swiftc_checker')
  finish
endif
let g:loaded_syntastic_swift_swiftc_checker = 1

if !exists('g:syntastic_swift_swiftc_executable')
  let g:syntastic_swift_swiftc_executable = 'swiftc'
endif

if !exists('g:syntastic_swift_swiftc_arguments')
  let g:syntastic_swift_swiftc_arguments = '-parse'
endif

let s:save_cpo = &cpo
set cpo&vim

function! SyntaxCheckers_swift_swiftc_IsAvailable() dict
  "if !executable(self.getExec())
  "  return 0
  "endif
  return 1

endfunction

function! SyntaxCheckers_swift_swiftc_GetLocList() dict
  let makeprg = self.makeprgBuild({
        \ 'fname': '',
        \ 'args': g:syntastic_swift_swiftc_arguments })

  let errorformat =
        \ '%f:%l:%c: error: %m'

  return SyntasticMake({
        \ 'makeprg': makeprg,
        \ 'errorformat': errorformat })
endfunction

call g:SyntasticRegistry.CreateAndRegisterChecker({
    \ 'filetype': 'swift',
    \ 'name': 'swiftc',
    \ 'exec': g:syntastic_swift_swiftc_executable })

let &cpo = s:save_cpo
unlet s:save_cpo

It doesn't work, though.

@sdondley
Copy link
Author

sdondley commented Feb 21, 2020

OK, I did a little bit more noodling and got something working:

if exists('g:loaded_syntastic_swift_swiftc_checker')
  finish
endif
let g:loaded_syntastic_swift_swiftc_checker = 1

if !exists('g:syntastic_swift_swiftc_executable')
  let g:syntastic_swift_swiftc_executable = 'swiftc'
endif

if !exists('g:syntastic_swift_swiftc_arguments')
  let g:syntastic_swift_swiftc_arguments = ''
endif

let s:save_cpo = &cpo
set cpo&vim

function! SyntaxCheckers_swift_swiftc_IsAvailable() dict
  "if !executable(self.getExec())
  "  return 0
  "endif
  return 1

endfunction

function! SyntaxCheckers_swift_swiftc_GetLocList() dict
    let file = fnameescape(expand("%:p"))
  let makeprg = self.makeprgBuild({
        \ 'fname': file,
        \ 'args': [''] })

  let errorformat = '%f:%l:%c: error: %m,%W%f:%l:%c: warning: %m,%Z%\s%#^~%#,%-G%.%#'

  return SyntasticMake({
        \ 'makeprg': makeprg,
        \ 'errorformat': errorformat })
endfunction

call g:SyntasticRegistry.CreateAndRegisterChecker({
    \ 'filetype': 'swift',
    \ 'name': 'swiftc',
    \ 'exec': g:syntastic_swift_swiftc_executable })

let &cpo = s:save_cpo
unlet s:save_cpo

Are you interested in having me create a patch for this? I'm sure what I have needs to be fixed up and better integrated with your plugin, though.

@keith
Copy link
Owner

keith commented Feb 22, 2020

Yep looks good! If you put it in that directory named swiftc.vim it should "just work" if you enable it in your syntastic config

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

No branches or pull requests

2 participants