Skip to content

Commit

Permalink
Code action support (vala-lang#237)
Browse files Browse the repository at this point in the history
Implement `textDocument/codeAction` with an action to convert integer literals to hexdecimal and octal.

Co-authored-by: Princeton Ferro <[email protected]>
  • Loading branch information
JCWasmx86 and Prince781 authored Apr 9, 2022
1 parent ef22ab2 commit 3a62835
Show file tree
Hide file tree
Showing 7 changed files with 735 additions and 4 deletions.
1 change: 1 addition & 0 deletions plugins/gnome-builder/vala_langserv.plugin.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Copyright=Copyright © 2020

Loader=python3
Module=vala_langserv
X-Code-Action-Languages=vala
X-Completion-Provider-Languages=vala
X-Diagnostic-Provider-Languages=vala
X-Formatter-Languages=vala
Expand Down
4 changes: 4 additions & 0 deletions plugins/gnome-builder/vala_langserv.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ def do_prepare(self):
self.props.priority = 100
VlsService.bind_client(self)

class VlsCodeActionProvider(Ide.LspCodeActionProvider):
def do_load(self):
VlsService.bind_client(self)

if hasattr(Ide, 'LspSearchProvider'): # for earlier versions of Builder
class VlsSearchProvider(Ide.LspSearchProvider):
def do_load(self, context):
Expand Down
67 changes: 67 additions & 0 deletions src/codeaction/baseconverteraction.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* baseconverteraction.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 Lsp;
using Gee;

/**
* The base converter code action allows to convert a constant to a different base.
* For example:
* ```vala
* int x = 10; // actions: convert to 0xA or 012
* ```
*/
class Vls.BaseConverterAction : CodeAction {
public BaseConverterAction (Vala.IntegerLiteral lit, VersionedTextDocumentIdentifier document) {
string val = lit.value;
// bool signed = lit.type_suffix[0] == 'u' || lit.type_suffix[0] == 'U';
bool negative = false;
if (val[0] == '-') {
negative = true;
val = val[1:];
}
var workspace_edit = new WorkspaceEdit ();
var document_edit = new TextDocumentEdit () {
textDocument = document
};
var text_edit = new TextEdit () {
range = new Range.from_sourceref (lit.source_reference)
};
if (val.has_prefix ("0x")) {
// base 16 -> base 8
val = val[2:];
text_edit.newText = "%s%#llo".printf (negative ? "-" : "", ulong.parse (val, 16));
this.title = "Convert hexadecimal value to octal";
} else if (val[0] == '0') {
// base 8 -> base 10
val = val[1:];
text_edit.newText = "%s%#lld".printf (negative ? "-" : "", ulong.parse (val, 8));
this.title = "Convert octal value to decimal";
} else {
// base 10 -> base 16
text_edit.newText = "%s%#llx".printf (negative ? "-" : "", ulong.parse (val));
this.title = "Convert decimal value to hexadecimal";
}
document_edit.edits.add (text_edit);
workspace_edit.documentChanges = new ArrayList<TextDocumentEdit> ();
workspace_edit.documentChanges.add (document_edit);
this.edit = workspace_edit;
}
}
Loading

0 comments on commit 3a62835

Please sign in to comment.