-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- AddThrowsDeclaration to add an error to the declaration of a method/constructor - AddTryCatch around statements - AddTryCatch around variable declarations
- Loading branch information
Showing
7 changed files
with
255 additions
and
4 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* addthrowsdeclaration.vala | ||
* | ||
* Copyright 2022 JCWasmx86 <[email protected]> | ||
* | ||
* This file is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This file is distributed in the hope that it will be useful, but | ||
* WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* SPDX-License-Identifier: LGPL-3.0-or-later | ||
*/ | ||
|
||
using Gee; | ||
using Lsp; | ||
|
||
class Vls.AddThrowsDeclaration : CodeAction { | ||
public AddThrowsDeclaration (VersionedTextDocumentIdentifier document, string error_name, Vala.CodeNode where) { | ||
|
||
var error_types = new Vala.HashSet<Vala.DataType> (); | ||
if (where is Vala.Constructor) | ||
((Vala.Constructor)where).body.get_error_types (error_types, null); | ||
else | ||
where.get_error_types (error_types, null); | ||
|
||
var sb = new StringBuilder (); | ||
if (error_types.is_empty) { | ||
sb.append (" throws "); | ||
} else { | ||
sb.append (", "); | ||
} | ||
sb.append (error_name); | ||
sb.append (" "); | ||
var sref = where.source_reference; | ||
var range = new Range (); | ||
for (var i = sref.begin.line; i <= sref.end.line; i++) { | ||
var line = sref.file.get_source_line (i); | ||
if (line.contains ("{")) { | ||
var idx = line.index_of ("{"); | ||
range.start = new Lsp.Position () { | ||
line = i - 1, | ||
character = idx - 1, | ||
}; | ||
range.end = new Lsp.Position () { | ||
line = i - 1, | ||
character = idx - 1, | ||
}; | ||
} | ||
} | ||
var workspace_edit = new WorkspaceEdit (); | ||
var document_edit = new TextDocumentEdit (document); | ||
var text_edit = new TextEdit (range); | ||
text_edit.range.end.character++; | ||
text_edit.newText = sb.str; | ||
document_edit.edits.add (text_edit); | ||
workspace_edit.documentChanges = new ArrayList<TextDocumentEdit> (); | ||
workspace_edit.documentChanges.add (document_edit); | ||
this.edit = workspace_edit; | ||
this.title = "Add to error list"; | ||
} | ||
} |
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,68 @@ | ||
/* AddTryCatchAssignmentAction.vala | ||
* | ||
* Copyright 2022 JCWasmx86 <[email protected]> | ||
* | ||
* This file is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This file is distributed in the hope that it will be useful, but | ||
* WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* SPDX-License-Identifier: LGPL-3.0-or-later | ||
*/ | ||
|
||
using Gee; | ||
using Lsp; | ||
|
||
class Vls.AddTryCatchAssignmentAction : CodeAction { | ||
public AddTryCatchAssignmentAction (VersionedTextDocumentIdentifier document, Vala.ArrayList<Vala.Variable> variables, string error_name, string indent, Vala.CodeNode rhs) { | ||
var sref = rhs.source_reference; | ||
var assignment_line = sref.file.get_source_line (sref.begin.line); | ||
var copied_indent = assignment_line.substring (0, assignment_line.length - assignment_line.chug ().length); | ||
var sb = new StringBuilder (); | ||
foreach (var v in variables) { | ||
sb.append (copied_indent).append (v.variable_type.to_string ()).append (" ").append (v.name).append (";\n"); | ||
} | ||
sb.append (copied_indent).append ("try {\n"); | ||
sb.append (copied_indent).append (indent).append (variables[0].name).append (" = "); | ||
var s1 = variables[0].initializer.source_reference; | ||
for (var i = s1.begin.line; i <= s1.end.line; i++) { | ||
var len = -1; | ||
var offset = 0; | ||
if (i == s1.begin.line && i != s1.end.line) { | ||
offset = s1.begin.column - 1; | ||
} else if (i == s1.end.line && i != s1.begin.line) { | ||
len = s1.end.column; | ||
} else if (i == s1.begin.line && i == s1.end.line) { | ||
offset = s1.begin.column - 1; | ||
len = s1.end.column - s1.begin.column + 1; | ||
} | ||
if (i != s1.begin.line) | ||
sb.append (copied_indent).append (indent); | ||
sb.append (s1.file.get_source_line (i).substring (offset, len).strip ()); | ||
sb.append (i == s1.end.line ? ";" : "").append ("\n"); | ||
} | ||
// TODO: Deduplicate error name | ||
sb.append (copied_indent).append ("} catch (").append (error_name).append (" e) {\n"); | ||
sb.append (copied_indent).append (indent).append ("error (\"Caught error ").append (error_name).append (": %s\", e.message);\n"); | ||
sb.append (copied_indent).append ("}\n"); | ||
var workspace_edit = new WorkspaceEdit (); | ||
var document_edit = new TextDocumentEdit (document); | ||
var text_edit = new TextEdit (new Range.from_sourceref (sref)); | ||
text_edit.range.start.character = 0; | ||
text_edit.range.end.character++; | ||
text_edit.newText = sb.str; | ||
document_edit.edits.add (text_edit); | ||
workspace_edit.documentChanges = new ArrayList<TextDocumentEdit> (); | ||
workspace_edit.documentChanges.add (document_edit); | ||
this.edit = workspace_edit; | ||
this.title = "Wrap with try-catch"; | ||
} | ||
} |
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,61 @@ | ||
/* addtrycatchstatementaction.vala | ||
* | ||
* Copyright 2022 JCWasmx86 <[email protected]> | ||
* | ||
* This file is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This file is distributed in the hope that it will be useful, but | ||
* WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* SPDX-License-Identifier: LGPL-3.0-or-later | ||
*/ | ||
|
||
using Gee; | ||
using Lsp; | ||
|
||
class Vls.AddTryCatchStatementAction : CodeAction { | ||
public AddTryCatchStatementAction (VersionedTextDocumentIdentifier document, string error_name, string indent, Vala.CodeNode node) { | ||
var sref = node.source_reference; | ||
var sb = new StringBuilder (); | ||
var line = sref.file.get_source_line (sref.begin.line); | ||
var copied_indent = line.substring (0, line.length - line.chug ().length); | ||
sb.append (copied_indent).append ("try {\n"); | ||
for (var i = sref.begin.line; i <= sref.end.line; i++) { | ||
var len = -1; | ||
var offset = 0; | ||
if (i == sref.begin.line && i != sref.end.line) { | ||
offset = sref.begin.column - 1; | ||
} else if (i == sref.end.line && i != sref.begin.line) { | ||
len = sref.end.column; | ||
} else if (i == sref.begin.line && i == sref.end.line) { | ||
offset = sref.begin.column - 1; | ||
len = sref.end.column - sref.begin.column + 1; | ||
} | ||
sb.append (copied_indent).append (indent); | ||
sb.append (sref.file.get_source_line (i).substring (offset, len).strip ()); | ||
sb.append (i == sref.end.line ? ";" : "").append ("\n"); | ||
} | ||
sb.append (copied_indent).append ("} catch (").append (error_name).append (" e) {\n"); | ||
sb.append (copied_indent).append (indent).append ("error (\"Caught error ").append (error_name).append (": %s\", e.message);\n"); | ||
sb.append (copied_indent).append ("}\n"); | ||
var workspace_edit = new WorkspaceEdit (); | ||
var document_edit = new TextDocumentEdit (document); | ||
var text_edit = new TextEdit (new Range.from_sourceref (sref)); | ||
text_edit.range.start.character = 0; | ||
text_edit.range.end.character++; | ||
text_edit.newText = sb.str; | ||
document_edit.edits.add (text_edit); | ||
workspace_edit.documentChanges = new ArrayList<TextDocumentEdit> (); | ||
workspace_edit.documentChanges.add (document_edit); | ||
this.edit = workspace_edit; | ||
this.title = "Wrap with try-catch"; | ||
} | ||
} |
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