This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 646
Add live error feedback as you type #903
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a76f8d2
Add live as-you-type error reporting
tylerb 4335c9f
Fix linter errors
tylerb 910cd8d
Correct properties so validation will work
tylerb 0f65407
Do not install gotype-live if not enabled
tylerb f57bc9c
Preserve existing non-error diagnostics
tylerb 8ce24b6
Fix lint errors
tylerb 4b51004
Clean indentation
tylerb 816b8c4
Add null/undefined/auto-save checks
tylerb aaf028a
Use separate diagnosticCollections for warn/err
tylerb b6d764c
update to setting description
ramya-rao-a File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
'use strict'; | ||
|
||
import vscode = require('vscode'); | ||
import { byteOffsetAt, getBinPath } from './util'; | ||
import cp = require('child_process'); | ||
import path = require('path'); | ||
import { promptForMissingTool } from './goInstallTools'; | ||
import { errorDiagnosticCollection } from './goMain'; | ||
|
||
// Interface for settings configuration for adding and removing tags | ||
interface GoLiveErrorsConfig { | ||
delay: number; | ||
enabled: boolean; | ||
} | ||
|
||
let runner; | ||
|
||
export function goLiveErrorsEnabled() { | ||
let goConfig = <GoLiveErrorsConfig>vscode.workspace.getConfiguration('go')['liveErrors']; | ||
if (goConfig === null || goConfig === undefined || !goConfig.enabled) { | ||
return false; | ||
} | ||
let autoSave = vscode.workspace.getConfiguration('files')['autoSave']; | ||
if (autoSave !== null && autoSave !== undefined && autoSave !== 'off') { | ||
return false; | ||
} | ||
return goConfig.enabled; | ||
} | ||
|
||
// parseLiveFile runs the gotype command in live mode to check for any syntactic or | ||
// semantic errors and reports them immediately | ||
export function parseLiveFile(e: vscode.TextDocumentChangeEvent) { | ||
if (e.document.isUntitled) { | ||
return; | ||
} | ||
if (e.document.languageId !== 'go') { | ||
return; | ||
} | ||
if (!goLiveErrorsEnabled()) { | ||
return; | ||
} | ||
|
||
if (runner != null) { | ||
clearTimeout(runner); | ||
} | ||
runner = setTimeout(function(){ | ||
processFile(e); | ||
runner = null; | ||
}, vscode.workspace.getConfiguration('go')['liveErrors']['delay']); | ||
} | ||
|
||
// processFile does the actual work once the timeout has fired | ||
function processFile(e: vscode.TextDocumentChangeEvent) { | ||
let uri = e.document.uri; | ||
let gotypeLive = getBinPath('gotype-live'); | ||
let fileContents = e.document.getText(); | ||
let fileName = e.document.fileName; | ||
let args = ['-e', '-a', '-lf=' + fileName, path.dirname(fileName)]; | ||
let p = cp.execFile(gotypeLive, args, (err, stdout, stderr) => { | ||
if (err && (<any>err).code === 'ENOENT') { | ||
promptForMissingTool('gotype-live'); | ||
return; | ||
} | ||
|
||
errorDiagnosticCollection.delete(uri); | ||
|
||
if (err) { | ||
// we want to take the error path here because the command we are calling | ||
// returns a non-zero exit status if the checks fail | ||
let diagnostics = []; | ||
|
||
stderr.split('\n').forEach(error => { | ||
if (error === null || error.length === 0) { | ||
return; | ||
} | ||
// extract the line, column and error message from the gotype output | ||
let [_, line, column, message] = /^.+:(\d+):(\d+):\s+(.+)/.exec(error); | ||
|
||
let range = new vscode.Range(+line - 1, +column, +line - 1, +column); | ||
let diagnostic = new vscode.Diagnostic(range, message, vscode.DiagnosticSeverity.Error); | ||
diagnostics.push(diagnostic); | ||
}); | ||
|
||
errorDiagnosticCollection.set(uri, diagnostics); | ||
} | ||
}); | ||
p.stdin.end(fileContents); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know it is closed but to be honest, I don't see a reason why autosave is checked at all. I use autosave onFocusChange and why would it be clashing with a linter running after some delay? Even autosave afterDelay makes little sense to check as it may just be some much higher value than linter delay.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was erring on the side of caution.
Good point on the fact that auto save onFocusChange and onWindowChange has no affect whatsoever.
I was concerned with the autosave on delay. Then you have build and gotype both running and giving essentially similar results and overriding each other.
On second thoughts, it is definitely being over cautious. We can remove the check in the next update. And leave it up to the user to choose between the 2 features if the experience is not pleasant
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!