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

Add syntax highlighting and LSP configuration for Vim/Neovim #1518

Merged
merged 8 commits into from
Sep 13, 2023
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
19 changes: 17 additions & 2 deletions editors/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,20 @@ To configure YAML editor tabs for schema validation, install the [YAML plugin](h

## Vim and Neovim

Currently there is neither highlighting nor LSP support for Vim,
but we would be happy to [accept a contribution](../CONTRIBUTING.md).
Add the following lines to your Vim/Neovim configuration file for files with the `.sw` extension to be recognized as `swarm` programs:


`init.vim`:

`au BufRead,BufNewFile *.sw setfiletype swarm`


`init.lua`:

`vim.cmd[[au BufRead,BufNewFile *.sw setfiletype swarm]]`


Basic syntax highlighting is available for both Vim and Neovim. To make use of this capability, copy [swarm.vim](vim/swarm.vim) to the `syntax` directory in your Vim or Neovim configuration directory.


An LSP configuration leveraging Neovim's native LSP client is also available. It only works with Neovim. To enable it, copy [swarm.lua](vim/swarm.lua) to `after/ftplugin` in your Neovim configuration directory.
8 changes: 8 additions & 0 deletions editors/vim/swarm.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
if vim.fn.executable('swarm') == 1 then
vim.lsp.start({
name = 'Swarm Language Server',
cmd = { 'swarm', 'lsp' },
})
end


24 changes: 24 additions & 0 deletions editors/vim/swarm.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
syn keyword Keyword def end let in require
syn keyword Builtins self parent base if inl inr case fst snd force undefined fail not format chars split charat tochar key
syn keyword Command noop wait selfdestruct move backup push stride turn grab harvest ignite place give equip unequip make has equipped count drill use build salvage reprogram say listen log view appear create halt time scout whereami waypoint detect resonate density sniff chirp watch surveil heading blocked scan upload ishere isempty meet meetall whoami setname random run return try swap atomic instant installkeyhandler teleport as robotnamed robotnumbered knows
syn keyword Direction east north west south down forward left back right
syn keyword Type int text dir bool cmd void unit actor


syn match Comment "//.*$"
syn region MultilineComment start="/\*" end="\*/"
syn match Brackets "[\[\]\(\)\{\}]"
syn match Colon ":"
syn match String "\".*\""
syn match Number "\<[-]\=\d\+\>"

hi def link Keyword Statement
hi def link Builtins Keyword
hi def link Command Function
hi def link Direction Function
hi def link Comment Comment
hi def link MultilineComment Comment
hi def link Brackets Keyword
hi def link Colon Keyword
hi def link String String
hi def link Number Number
10 changes: 9 additions & 1 deletion src/Swarm/Doc/Gen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ data GenerateDocs where

-- | An enumeration of the editors supported by Swarm (currently,
-- Emacs and VS Code).
data EditorType = Emacs | VSCode
data EditorType = Emacs | VSCode | Vim
deriving (Eq, Show, Enum, Bounded)

-- | An enumeration of the kinds of cheat sheets we can produce.
Expand Down Expand Up @@ -160,6 +160,13 @@ generateEditorKeywords = \case
T.putStrLn $ keywordsDirections VSCode
putStrLn "\nOperators:"
T.putStrLn operatorNames
Vim -> do
putStr "syn keyword Builtins "
T.putStr $ builtinFunctionList Vim
putStr "\nsyn keyword Command "
T.putStr $ keywordsCommands Vim
putStr "\nsyn keyword Direction "
T.putStrLn $ keywordsDirections Vim

commands :: [Const]
commands = filter Syntax.isCmd Syntax.allConst
Expand All @@ -177,6 +184,7 @@ editorList :: EditorType -> [Text] -> Text
editorList = \case
Emacs -> T.unlines . map ((" " <>) . quote)
VSCode -> T.intercalate "|"
Vim -> T.intercalate " "

constSyntax :: Const -> Text
constSyntax = Syntax.syntax . Syntax.constInfo
Expand Down
2 changes: 2 additions & 0 deletions swarm.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ extra-source-files: CHANGELOG.md
example/*.sw
editors/emacs/*.el
editors/vscode/syntaxes/*.json
editors/vim/*.vim
editors/vim/*.lua
data-dir: data/
data-files: *.yaml, worlds/*.world, scenarios/**/*.yaml, scenarios/**/*.txt, scenarios/**/*.sw, *.txt, test/language-snippets/**/*.sw

Expand Down
7 changes: 7 additions & 0 deletions test/integration/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -453,10 +453,17 @@ testEditorFiles =
, testTextInEmacs "commands" DocGen.keywordsCommands
, testTextInEmacs "directions" DocGen.keywordsDirections
]
, testGroup
"Vim"
[ testTextInVim "builtin" DocGen.builtinFunctionList
, testTextInVim "commands" DocGen.keywordsCommands
, testTextInVim "directions" DocGen.keywordsDirections
]
]
where
testTextInVSCode name tf = testTextInFile False name (tf VSCode) "editors/vscode/syntaxes/swarm.tmLanguage.json"
testTextInEmacs name tf = testTextInFile True name (tf Emacs) "editors/emacs/swarm-mode.el"
testTextInVim name tf = testTextInFile True name (tf Vim) "editors/vim/swarm.vim"
testTextInFile :: Bool -> String -> Text -> FilePath -> TestTree
testTextInFile whitespace name t fp = testCase name $ do
let removeLW' = T.unlines . map (T.dropWhile isSpace) . T.lines
Expand Down
Loading