Skip to content

Commit

Permalink
Much better handling of Syntax errors while you type
Browse files Browse the repository at this point in the history
  • Loading branch information
micoloth committed Feb 28, 2024
1 parent 1e4ba83 commit a52c738
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 43 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"color": "#ffffff",
"theme": "light"
},
"version": "0.1.5",
"version": "0.1.6",
"engines": {
"vscode": "^1.85.0"
},
Expand Down
17 changes: 1 addition & 16 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ const getTextInRanges = (ranges: AnnotatedRange[]): string[] => {

const getEditorCurrentText = (editor: TextEditor): { currentQuery: string; currentRange: Range | null } => {
// Currently not used..

if (!editor || !editor.document || editor.document.uri.scheme === 'output') {
return {
currentQuery: '',
Expand Down Expand Up @@ -855,27 +855,14 @@ const HighlightOutdatedCurrent = window.createTextEditorDecorationType({
});

let updateDecorations = async (editor: TextEditor, ranges_out: AnnotatedRange[]) => {
// if (!Config.highlightQuery) return;
if (
!editor ||
!editor.document ||
editor.document.uri.scheme === 'output'
// || !this.registeredLanguages.includes(editor.document.languageId) // <--- COMES FROM this.registeredLanguages = Config.codelensLanguages; // See Config below
) {
return;
}
try {
// const { currentRange, currentQuery } = getEditorCurrentText(editor);
// if (!currentRange || !currentQuery) return;
// let command = getCommandToGetRangeToSelectFromPython(currentRange.start.line.toString());

// Save the range associated with this editor:
// 1. Get the Kernel uuid:
// let kernel_uuid = editor.document.uri;
// // 2. Set current editor ranges in global state:
// await globalState.update(kernel_uuid.toString() + '_ranges', ranges_out);

// Set highlight on all the ranges in ranges_out with state == 'synced'
let sync_ranges = ranges_out.filter((r) => r.state == 'synced' && !r.current).map((r) => r.range);
editor.setDecorations(HighlightSynced, sync_ranges);
let sync_curr_ranges = ranges_out.filter((r) => r.state == 'synced' && r.current).map((r) => r.range);
Expand Down Expand Up @@ -930,7 +917,6 @@ export class CellCodelensProvider implements vscode.CodeLensProvider {
): vscode.CodeLens[] | Thenable<vscode.CodeLens[]> {
let editor = vscode.window.activeTextEditor;
if (editor && this.range && editor.document.uri == document.uri) {
// Current line:
this.codeLenses = [
new vscode.CodeLens(new vscode.Range(this.range.start.line, 0, this.range.end.line, 0), {
title: 'sync upstream',
Expand Down Expand Up @@ -964,7 +950,6 @@ export class CellCodelensProvider implements vscode.CodeLensProvider {
}

export class InitialCodelensProvider implements vscode.CodeLensProvider {
private started_at_least_once: vscode.CodeLens[] = [];
public provideCodeLenses(
document: vscode.TextDocument,
token: vscode.CancellationToken
Expand Down
23 changes: 11 additions & 12 deletions src/reactive_python_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -935,11 +935,8 @@ def update_dag(self, code: str, current_line: Optional[int] = None):
except SyntaxError as e:
# Get the line number of the syntax error:
line_number = e.lineno
self.syntax_error_range = [[line_number-1 if line_number else line_number, line_number, "syntaxerror", "current"]]
if line_number and not (current_line and -2 < line_number - current_line < 2):
return json.dumps(self.syntax_error_range)
else:
return []
self.syntax_error_range = [line_number-1 if line_number else line_number, line_number]
return

try:
dagnodes = self.ast_to_dagnodes(ast_tree, code)
Expand All @@ -955,21 +952,23 @@ def update_dag(self, code: str, current_line: Optional[int] = None):

if not self.locked_for_execution: # Should never happen, but better be careful
self.current_dag = new_dag
return True
return
except Exception as e:
print(e)
# raise e
return e

def update_dag_and_get_ranges(self, code: Optional[str] = None, current_line: Optional[int] = None, get_upstream=True, get_downstream=True, stale_only=False, include_code=False):
if code and not self.locked_for_execution:
result = self.update_dag(code)
if result != True:
return result
errors = self.update_dag(code)
if errors:
return errors
if self.syntax_error_range:
line_number = self.syntax_error_range[0][0]
if line_number and not (current_line and -2 < line_number - current_line < 2):
return json.dumps(self.syntax_error_range)
line_number = self.syntax_error_range[0]
other_selection = line_number and current_line and not -2 <= (line_number - current_line) <= 2
if other_selection:
syntax_error_range = [[line_number-1 if line_number else line_number, line_number, "syntaxerror", ""]]
return json.dumps(syntax_error_range)
else:
return []
if self.current_dag:
Expand Down
23 changes: 11 additions & 12 deletions src/reactive_python_engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -936,11 +936,8 @@ class ReactivePythonDagBuilderUtils__():
except SyntaxError as e:
# Get the line number of the syntax error:
line_number = e.lineno
self.syntax_error_range = [[line_number-1 if line_number else line_number, line_number, "syntaxerror", "current"]]
if line_number and not (current_line and -2 < line_number - current_line < 2):
return json.dumps(self.syntax_error_range)
else:
return []
self.syntax_error_range = [line_number-1 if line_number else line_number, line_number]
return
try:
dagnodes = self.ast_to_dagnodes(ast_tree, code)
Expand All @@ -956,21 +953,23 @@ class ReactivePythonDagBuilderUtils__():
if not self.locked_for_execution: # Should never happen, but better be careful
self.current_dag = new_dag
return True
return
except Exception as e:
print(e)
# raise e
return e
def update_dag_and_get_ranges(self, code: Optional[str] = None, current_line: Optional[int] = None, get_upstream=True, get_downstream=True, stale_only=False, include_code=False):
if code and not self.locked_for_execution:
result = self.update_dag(code)
if result != True:
return result
errors = self.update_dag(code)
if errors:
return errors
if self.syntax_error_range:
line_number = self.syntax_error_range[0][0]
if line_number and not (current_line and -2 < line_number - current_line < 2):
return json.dumps(self.syntax_error_range)
line_number = self.syntax_error_range[0]
other_selection = line_number and current_line and not -2 <= (line_number - current_line) <= 2
if other_selection:
syntax_error_range = [[line_number-1 if line_number else line_number, line_number, "syntaxerror", ""]]
return json.dumps(syntax_error_range)
else:
return []
if self.current_dag:
Expand Down

0 comments on commit a52c738

Please sign in to comment.