Skip to content

Commit e643c7b

Browse files
Added checkbox & functionality to support further analysis via https://staticcheck.io (this is provided through gopls)
1 parent 33441b3 commit e643c7b

File tree

5 files changed

+35
-20
lines changed

5 files changed

+35
-20
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
## Version 0.4.1
2+
3+
- Added checkbox on preferences to use https://staticcheck.io/ to do additional code analysis.
4+
15
## Version 0.4.0
26

37
- Nova 5.1 is out and it supports Language Servers much better! Enjoy the quasi-IDE environment!
48
- Added support for editing `go.mod` files. Note: If you've installed [VLC](https://www.videolan.org/vlc/), `.mod` files are thought to be audio files, thus they're not easily opened with Nova (added `Tests/go.mod.sample` to test it out).
59
- *Big* cleaning up of the syntax definition file; all regular expressions have been tested, one by one, and fixed in uncountable cases. Even capturing complex numbers works! Much better syntax highlighting. Incidentally, issue [_#1_](https://github.com/GwynethLlewelyn/Go.novaextension/issues/1) got fixed, after 7 months...
6-
- The **Editor > Go** menu options for text formatting and reorganising imports work about 80% of the cases. They are now interfacing correctly with `gopls` and retrieving proper replacement information (who'd think that Google would send all updates _in reverse order_?!). So, no need to run any additional external commands for those operations; `gopls` is _supposed_ to do all the work from now on.
10+
- The **Editor > Go** menu options for text formatting and reorganising imports work about 80% of the cases. They are now interfacing correctly with `gopls` and retrieving proper replacement information (who'd think that Google would send all updates _in reverse order_?!). So, no need to run any additional external commands for those operations; `gopls` is _supposed_ to do all the work from now on.
711
- Sadly, the option to format/optimise imports on _saving_ is **not** functional. There is an **experimental** checkbox for it on the preferences, but **DO. NOT. CHECK. IT.**. It will _literally_ destroy your files when saving them. Basically, it runs the formatter twice, for some unfathomable reason, and I couldn't find a way to prevent that.
812

913
### To-do:

README.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ Additionally, much of the more intricate and obscure issues have been solved by
1515

1616
- [@apexskier](https://github.com/apexskier) (Cameron Little)
1717

18-
**Note:** This is still an **alpha** version with working syntax highlighting and extra goodies from using the [Language Server Protocol](https://microsoft.github.io/language-server-protocol/), but it is plagued by some unstability issues, often leading to a crash. See below for some guidelines.
18+
**Note:** v0.4.1 is still an **alpha** version with working syntax highlighting and extra goodies from using the [Language Server Protocol](https://microsoft.github.io/language-server-protocol/), but it is plagued with some unstability issues, often leading to a crash. See below for some guidelines.
1919

20-
If nothing here works for you, you might try to use Spicer Matthews' own [GoTools](nova://extension?id=cloudmanic.GoTools "This link will open only in Nova") extension. The syntax highlighting should be similar — and that extension should not crash!
20+
[@jfieber](https://github.com/jfieber) added a 'Go' menu from which some functionality can be selected; in many cases, it will attempt to do a re-formatting according to Go's best practices, as well as organise imports. This is not working 100% of the time, though (and we still don't know why).
21+
22+
Worst case scenario: if _nothing_ here works for you, you might try to use Spicer Matthews' own [GoTools](nova://extension?id=cloudmanic.GoTools "This link will open only in Nova") extension. The syntax highlighting should be similar — and that extension should _not_ crash!
2123

2224
# Installation
2325

@@ -33,6 +35,8 @@ If not, you can install it as described [here](https://github.com/golang/tools/b
3335

3436
Then tick the *Enable Language Server* checkbox in the Preferences for this extension. If `gopls` is (correctly) installed in your search `PATH`, the extension should find it. If it is elsewhere, set that where in the Preferences for this extension as well. Note that Nova's language server support and `gopls` are both new and evolving. Many things don't work quite right yet<sup id="a2">[2](#f2)</sup>.
3537

38+
When checking for syntax errors, `gopls` can optionally use https://staticcheck.io/ for additionally analysis; there is a checkbox on Preferences to allow the extra checking.
39+
3640
**WARNING:** (aye, again) If you click on *Format/Organise Imports when saving file* to get `gopls` to automatically reformat your code when saving, be warned: it _really_ does corrupt the files! I'm _not_ joking. The option is there just because I'm hoping to try it out until I find a way to fix the issue.
3741

3842
More information on the capabilities of `gopls` as well as its design/implementation considerations can be found [on GitHub](https://github.com/golang/tools/blob/master/gopls/doc/design/design.md). As Nova gets better and better integration with Language Servers, and as Google also improves `gopls` to be more compliant with Microsoft's LSP definition, a lot of functionality will appear 'automagically' by simply upgrading Nova or `gopls`.
@@ -47,13 +51,13 @@ Additionally, I've included a Syntax template for Go Templates. It's almost exac
4751

4852
The new, incuded syntax highlighter for `go.mod` files was mostly an experiment. It works quite well, though, since the language definition for that is so simple.
4953

50-
Although the current version includes a Completions file, I don't see any kind of completion happening (except for closing braces). I've tried to figure out how the syntax of the Completions file works, but it's even more obscure (to me) than the actual Syntax file. The two are also possibly related — through common identifiers — but I fail to understand exactly how the two 'play' together.
54+
Although the current version includes a Completions file, I don't see any kind of completion happening (except for closing braces). I've tried to figure out how the syntax of the Completions file works, but it's even more obscure (to me) than the actual Syntax file. The two are also possibly related — through common identifiers — but I fail to understand exactly how the two 'play' together. I've gotten access to a few more resources explaining this format, so maybe I'll give it another try.
5155

5256
Also, Nova — unlike Coda — allows extensions to use the [Language Server Protocol](https://langserver.org/) (like their closest competitors do), a Microsoft-promoted effort enabling a '*write once, reuse often*' philosophy for writers of syntax checkers. In other words, the hard work is put into creating a _functioning_ LSP 'server' (basically an application that reads a file in a specific language, or parts of a file, and figures out if it has any syntax errors) by the community of syntax checkers. _Then_ editor plugins (of all kinds, not only desktop apps) can interface with such a server in order to get syntax checking. In theory, it's a very easy way to quickly expand the ability for a code editor to accept gazillions of languages without requiring the developers to spend days and nights coding as many syntax checkers as possible. With new languages coming up all the time, many becoming fashionable for a few years and disappearing shortly afterwards, this method ensures that editor developers are not wasting their precious time. It also means that updating syntax checkers for upcoming new versions of a specific language will not require any coding — just upgrade the LSP server. And, finally, it also means that _all_ code editors will check syntax in the _same, predictable_ way (and also that _all_ will get upgraded to the latest version of the syntax almost immediately), which means that, if you switch editors, the 'new' editor will _already_ support the latest version of your favourite programming language _and_ syntax checking will work in pretty much the same way...
5357

5458
Therefore, this extension can optionally use the official (Google-supported) [Go Language Server](https://github.com/golang/tools/blob/master/gopls/README.md) if you install it as described in the Installation section above. Thanks to recent improvements in Nova, it's now almost fully functional. Enjoy 😌
5559

56-
LSP is still very quirky. Microsoft developed the LSP specifically for Visual Studio Code, their free, open-source, cross-platform editor, thus allowing non-Microsoft-specific languages to be as easily integrated with their editor. However, the Microsoft specifications are *very* Microsoft-centric, and the Google team behind `gopls` grumbles and complains about several ambiguous descriptions in the protocol, and the choices they've made to implement the (complex) specifications. Sadly, though, those specifications are *very* Google-centric (notice the pattern?) and they don't match easily with the Microsoft ones. And on top of all that, we have to deal with Panic's own implementation of LSP support — which is (at the time of writing!) a bit obscure, a bit opaque, and not exactly super-well-documented (namely, at least for me, it's not obvious what Nova does on its own and what the extension developer has to do). We have to take hints from the very few others who travelled the same road and try to figure out how they've done it.
60+
The LSP specification is still very quirky. Microsoft developed the LSP specifically for Visual Studio Code, their free, open-source, cross-platform editor, thus allowing non-Microsoft-specific languages to be as easily integrated with their editor. However, the Microsoft specifications are *very* Microsoft-centric, and the Google team behind `gopls` grumbles and complains about several ambiguous descriptions in the protocol, and the choices they've made to implement the (complex) specifications. Sadly, though, those specifications are *very* Google-centric (notice the pattern?) and they don't match easily with the Microsoft ones. And on top of all that, we have to deal with Panic's own implementation of LSP support — which is (at the time of writing!) a bit obscure, a bit opaque, and not exactly super-well-documented (namely, at least for me, it's not obvious what Nova does on its own and what the extension developer has to do). We have to take hints from the very few others who travelled the same road and try to figure out how they've done it.
5761

5862
Note that Nova logs many interactions with the language server in the Extension Console, and if you run this extension in "developer mode" (see the *Extension Development* item in Nova's *General* preferences) `gopls` will write logs to `/tmp/gopls.log` but these can be quite a bit cryptic.
5963

@@ -64,9 +68,10 @@ Suggestions and bug reports are more than welcome, but please take into account
6468
# Well-known bugs
6569

6670
1. Right now, the one issue that baffles me most is why the formatter is called _twice_ at the moment of saving the files to disk/cloud. This will naturally enough trash the _original_ file; so do **NOT** click on the checkbox unless, of course, you wish to help to debug this!
67-
2. It _still_ makes me wonder why we *are* getting Markdown from `gopls` but somehow Nova is confused and renders it as plain text (which is... weird). I think I managed to make `gopls` change from Markdown to plain text — so that Nova might at least display a simple one-liner, albeit unformatted. This is suboptimal, because `gopls` is nowadays _much_ better at providing explanations — alas, for some reason, Nova cannot 'understand' whatever Markdown is handed overy by `gopls`...
68-
3. The extension crashes quite often, especially when in Development mode. There are no 'obvious' errors showing up on the console or the error logs, so this has been hard to track down (I suspect that there is some cleaning up that is missing). If you're plagued with these crashes, just uncheck the *Enable Language Server* checkbox on the Preferences for this extension. You will still get Go syntax highlighting, but the additional goodies provided by LSP will be turned off.
69-
4. There are a few hooks allowing you to *theoretically* run some post-formatting tools to comply with Google's style guidelines for Go, as well as a way to check that all imports are properly referenced, etc. With this version, there is no documentation yet for *how* to activate those extra features, but hopefully this will be addressed in the very near future... fingers crossed!
71+
2. On the other hand, the 'Go' menu (either from the top bar or by right-clicking on the text) runs the formatter and/or the imports organiser most of the time, without crashing, or garbling the file. Sometimes it does nothing; sometimes you have to select the whole range you wish to format in order to get it working. This is still a bit unpredictable, although I can't understand why it doesn't work _some_ times, while in _most_ cases, it has no issue whatsoever (even on the same, unchanged code!).
72+
3. Panic [recently acknowledged that there are some limitations on rendering Markdown on LSP hover tips](https://devforum.nova.app/t/lsp-tooltip-markdown-support/608/2). We're all hoping to see some upcoming release with a fix! Until then, the hover tips will be rendered in plain text instead.
73+
4. The extension crashes quite often, especially when in Development mode. There are no 'obvious' errors showing up on the console or the error logs, so this has been hard to track down (I suspect that there is some cleaning up that is missing). If you're plagued with these crashes, just _un_check the *Enable Language Server* checkbox on the Preferences for this extension. You will still get Go syntax highlighting, but the additional goodies provided by LSP will be turned off.
74+
5. There are a few hooks allowing you to *theoretically* run some post-formatting tools to comply with Google's style guidelines for Go, as well as a way to check that all imports are properly referenced, etc. With this version, there is no documentation yet for *how* to activate those extra features, but hopefully this will be addressed in the very near future... fingers crossed!
7075

7176
## My GPG Fingerprint
7277

Scripts/lsp.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,14 @@ exports.NovaPositionsFromLSPRangeElement = function(document, lspLine, lspCharac
127127
if (lspLine === lineIndex) {
128128
position = chars + lspCharacter;
129129
// break; // we can save a few cycles
130+
return position; // get out of the loop as early as possible
130131
}
131132
chars += lineLength;
132133
}
133134
/* if (nova.inDevMode()) {
134135
console.info(`NovaPositionsFromLSPRangeElement() — LSP Line: ${lspLine}; LSP Column: ${lspCharacter}; Nova Position: ${position}`);
135136
}*/
136-
return position;
137+
return position;
137138
}
138139

139140
// ApplyTextEditsRevamped calculates if a bit of formatted has to be inserted, replaced, or removed.
@@ -145,7 +146,7 @@ exports.ApplyTextEditsRevamped = (editor, edits) => {
145146
// very, very inefficient for now, but we will improve later using just one loop (gwyneth 20210406)
146147
var startPosition = exports.NovaPositionsFromLSPRangeElement(editor.document, e.range.start.line, e.range.start.character);
147148
var endPosition = exports.NovaPositionsFromLSPRangeElement(editor.document, e.range.end.line, e.range.end.character);
148-
149+
149150
if (e.newText == null || e.newText == undefined || e.newText == "") { // this means we're going to _delete_ the characters in the range, and that the range must be valid
150151
var deletedRange = new Range(startPosition, endPosition -1);
151152
tee.delete(deletedRange);

Scripts/main.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,8 @@ class GoLanguageServer {
108108
var clientOptions = {
109109
syntaxes: ['go'],
110110
initializationOptions: {
111-
// "hoverKind": "SingleLine", // one of these ought to do the trick
112-
"hoverKind": "SingleLine",
113-
// "ui.documentation.hoverKind": "SingleLine", // I know that "SingleLine" works — other options seem to _always_ trigger Markdown (gwyneth 20210202)
114-
// "staticcheck": true, // using staticcheck.io for further analysis (gwyneth 20210406)
115-
// "ui.completion.usePlaceHolders": true, // ...whatever this does...
111+
"hoverKind": "SingleLine", // I know that "SingleLine" works — other options seem to _always_ trigger Markdown (gwyneth 20210202) for the information, which somehow Nova dislikes and does not render (gwyneth 20210407)
112+
"staticcheck": nova.config.get('go-nova.use-staticcheck', 'boolean'), // using staticcheck.io for further analysis (gwyneth 20210407)
116113
"usePlaceHolders": true // trying out which one works (gwyneth 20210203)
117114
}
118115
};
@@ -155,11 +152,12 @@ class GoLanguageServer {
155152
if (editor.document.syntax === 'go') {
156153
if (nova.config.get('go-nova.format-on-save', 'boolean')) {
157154
console.info('Entering FormatOnSave for "' + editor.document.uri + '"...');
158-
try {
159-
formatFile(editor);
155+
try {
156+
// nova.commands.invoke("go.formatFile", editor); // this is how @apexskier calls the function (20210408)
157+
formatFile(editor)
160158
} catch(err) {
161159
console.error("Re-formatting failed miserably");
162-
}
160+
}
163161
}
164162
}
165163
}, this);
@@ -250,7 +248,7 @@ function formatFile(editor) {
250248
uri: editor.document.uri
251249
},
252250
options: { // https://github.com/microsoft/language-server-protocol/blob/gh-pages/_specifications/specification-3-17.md#document-formatting-request--leftwards_arrow_with_hook (gwyneth 20210406)
253-
tabSize: editor.tabLength,
251+
tabSize: editor.tabLength,
254252
insertSpaces: editor.softTabs != 0
255253
}
256254
};

extension.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"name": "Go",
44
"organization": "Gwyneth Llewelyn",
55
"description": "Go Language Definition for Nova",
6-
"version": "0.4.0",
6+
"version": "0.4.1",
77
"bugs": "https://github.com/GwynethLlewelyn/Go.novaextension/issues",
88
"repository": "https://github.com/GwynethLlewelyn/Go.novaextension",
99
"homepage": "https://gwynethllewelyn.net",
@@ -41,6 +41,13 @@
4141
"description": "When saving, use `gopls` to format file according to Go formatting standards and organize imports (the equivalent of executing `gofmt`, `goimports`, `goreturns`). Requires `gopls` language server to be active.\nWARNING: For experimental purposes ONLY! Currently it WILL DESTROY YOUR FILES!",
4242
"type": "boolean",
4343
"default": false
44+
},
45+
{
46+
"key": "go-nova.use-staticcheck",
47+
"title": "(Experimental) Use https://staticcheck.io for further analysis",
48+
"description": "Use `gopls` in conjunction with https://staticcheck.io for further analysis of the code. This also requires `gopls` language server to be active.",
49+
"type": "boolean",
50+
"default": false
4451
}
4552
],
4653
"commands": {

0 commit comments

Comments
 (0)