Commit b7d1e453 authored by Ben's avatar Ben
Browse files

Vala: use a consistent coding style

parent 0762adc4
......@@ -16,14 +16,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using GLib;
using Gtk;
using Vala;
namespace Ide
{
public class ValaCompletionItem: Ide.CompletionItem, Gtk.SourceCompletionProposal
{
namespace Ide {
public class ValaCompletionItem: Ide.CompletionItem, Gtk.SourceCompletionProposal {
static uint hash_seed;
internal Vala.Symbol symbol;
......@@ -34,8 +31,7 @@ namespace Ide
hash_seed = "IdeValaCompletionItem".hash ();
}
public ValaCompletionItem (Vala.Symbol symbol, Ide.ValaCompletionProvider provider)
{
public ValaCompletionItem (Vala.Symbol symbol, Ide.ValaCompletionProvider provider) {
this.symbol = symbol;
this.provider = provider;
......@@ -46,44 +42,42 @@ namespace Ide
this.build_label ();
}
public unowned string? get_icon_name ()
{
if (symbol is Vala.LocalVariable)
public unowned string? get_icon_name () {
if (symbol is Vala.LocalVariable) {
return "lang-variable-symbolic";
else if (symbol is Vala.Field)
} else if (symbol is Vala.Field) {
return "struct-field-symbolic";
else if (symbol is Vala.Subroutine)
} else if (symbol is Vala.Subroutine) {
return "lang-function-symbolic";
else if (symbol is Vala.Namespace)
} else if (symbol is Vala.Namespace) {
return "lang-include-symbolic";
else if (symbol is Vala.MemberAccess)
} else if (symbol is Vala.MemberAccess) {
return "struct-field-symbolic";
else if (symbol is Vala.Property)
} else if (symbol is Vala.Property) {
return "struct-field-symbolic";
else if (symbol is Vala.Struct)
} else if (symbol is Vala.Struct) {
return "lang-struct-symbolic";
else if (symbol is Vala.Class)
} else if (symbol is Vala.Class) {
return "lang-class-symbolic";
else if (symbol is Vala.Enum)
} else if (symbol is Vala.Enum) {
return "lang-enum-symbolic";
else if (symbol is Vala.EnumValue)
} else if (symbol is Vala.EnumValue) {
return "lang-enum-value-symbolic";
else if (symbol is Vala.Delegate)
} else if (symbol is Vala.Delegate) {
return "lang-typedef-symbolic";
}
return null;
}
public override bool match (string query, string casefold)
{
public override bool match (string query, string casefold) {
uint priority = 0;
bool result = Ide.CompletionItem.fuzzy_match (this.symbol.name, casefold, out priority);
this.set_priority (priority);
return result;
}
public void build_label ()
{
public void build_label () {
GLib.StringBuilder str = new GLib.StringBuilder ();
if (this.symbol is Vala.Method) {
......@@ -108,10 +102,11 @@ namespace Ide
break;
}
if (param.direction == ParameterDirection.OUT)
if (param.direction == ParameterDirection.OUT) {
str.append ("out ");
else if (param.direction == ParameterDirection.REF)
} else if (param.direction == ParameterDirection.REF) {
str.append ("ref ");
}
str.append_printf ("%s, ", param.variable_type.to_qualified_string (method.owner));
}
......@@ -137,8 +132,7 @@ namespace Ide
return this.label;
}
public string get_text ()
{
public string get_text () {
return this.symbol.name;
}
......@@ -150,8 +144,7 @@ namespace Ide
return null;
}
public uint hash ()
{
public uint hash () {
return this.symbol.name.hash () ^ hash_seed;
}
}
......
......@@ -16,23 +16,18 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using GLib;
using Ide;
using Vala;
namespace Ide
{
public class ValaCompletionProvider: Ide.Object,
Gtk.SourceCompletionProvider,
Ide.CompletionProvider
{
namespace Ide {
public class ValaCompletionProvider : Ide.Object,
Gtk.SourceCompletionProvider,
Ide.CompletionProvider {
internal string? query;
int line = -1;
int column = -1;
Ide.CompletionResults? results;
public void populate (Gtk.SourceCompletionContext context)
{
public void populate (Gtk.SourceCompletionContext context) {
Gtk.TextIter iter;
Gtk.TextIter begin;
......@@ -89,25 +84,26 @@ namespace Ide
cancellable,
out res_line,
out res_column);
if (res_line > 0 && res_column > 0) {
if (res_line > 0 && res_column > 0) {
this.line = res_line - 1;
this.column = res_column - 1;
}
Idle.add (() => {
if (!cancellable.is_cancelled ())
if (!cancellable.is_cancelled ()) {
this.results.present (this, context);
}
return false;
});
});
}
public bool match (Gtk.SourceCompletionContext context)
{
public bool match (Gtk.SourceCompletionContext context) {
Gtk.TextIter iter;
if (!context.get_iter (out iter))
if (!context.get_iter (out iter)) {
return false;
}
var buffer = iter.get_buffer () as Ide.Buffer;
......@@ -120,12 +116,14 @@ namespace Ide
* whitespace.
*/
if (context.activation != Gtk.SourceCompletionActivation.USER_REQUESTED) {
if (iter.starts_line () || !iter.backward_char () || iter.get_char ().isspace ())
if (iter.starts_line () || !iter.backward_char () || iter.get_char ().isspace ()) {
return false;
}
}
if (Ide.CompletionProvider.context_in_comment (context))
if (Ide.CompletionProvider.context_in_comment (context)) {
return false;
}
return true;
}
......@@ -134,8 +132,7 @@ namespace Ide
return "Vala";
}
public int get_priority ()
{
public int get_priority () {
return 200;
}
}
......
......@@ -19,14 +19,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using GLib;
using Gtk;
using Vala;
namespace Ide
{
public class ValaCompletion: GLib.Object
{
namespace Ide {
public class ValaCompletion : GLib.Object {
static Regex member_access;
static Regex member_access_split;
static Regex function_call;
......@@ -49,22 +46,21 @@ namespace Ide
public ValaCompletion (Vala.CodeContext context,
Vala.SourceLocation location,
string current_text,
Vala.Block? nearest)
{
Vala.Block? nearest) {
this.context = context;
this.location = location;
this.current_text = current_text;
this.nearest = nearest;
}
public GLib.List<Vala.Symbol>? run (ref Vala.SourceLocation start_pos)
{
public GLib.List<Vala.Symbol>? run (ref Vala.SourceLocation start_pos) {
MatchInfo match_info;
if (!member_access.match (current_text, 0, out match_info))
if (!member_access.match (current_text, 0, out match_info)) {
return null;
else if (match_info.fetch(0).length < 2)
} else if (match_info.fetch(0).length < 2) {
return null;
}
start_pos.line = this.location.line;
start_pos.column = this.location.column - (int)match_info.fetch (2).length;
......@@ -79,12 +75,12 @@ namespace Ide
return syms;
}
GLib.List<Vala.Symbol> lookup_symbol (Vala.Expression? inner, string name, bool prefix_match, Vala.Block? block)
{
GLib.List<Vala.Symbol> lookup_symbol (Vala.Expression? inner, string name, bool prefix_match, Vala.Block? block) {
var matching_symbols = new GLib.List<Vala.Symbol> ();
if (block == null)
if (block == null) {
return matching_symbols;
}
if (inner == null) {
for (var sym = (Vala.Symbol) block; sym != null; sym = sym.parent_symbol) {
......@@ -95,12 +91,13 @@ namespace Ide
matching_symbols.concat (symbol_lookup_inherited (ns.namespace_symbol, name, prefix_match));
}
} else if (inner.symbol_reference != null) {
matching_symbols.concat (symbol_lookup_inherited (inner.symbol_reference, name, prefix_match));
matching_symbols.concat (symbol_lookup_inherited (inner.symbol_reference, name, prefix_match));
} else if (inner is Vala.MemberAccess) {
var inner_ma = (Vala.MemberAccess) inner;
var matching = lookup_symbol (inner_ma.inner, inner_ma.member_name, false, block);
if (matching != null)
if (matching != null) {
matching_symbols.concat (symbol_lookup_inherited (matching.data, name, prefix_match));
}
} else if (inner is Vala.MethodCall) {
var inner_inv = (Vala.MethodCall) inner;
var inner_ma = inner_inv.call as Vala.MemberAccess;
......@@ -117,13 +114,13 @@ namespace Ide
GLib.List<Vala.Symbol> symbol_lookup_inherited (Vala.Symbol? sym,
string name,
bool prefix_match,
bool invocation = false)
{
bool invocation = false) {
GLib.List<Vala.Symbol> result = null;
// This may happen if we cannot find all the needed packages
if (sym == null)
if (sym == null) {
return result;
}
var symbol_table = sym.scope.get_symbol_table ();
......@@ -168,8 +165,7 @@ namespace Ide
return result;
}
Vala.Expression construct_member_access (string[] names)
{
Vala.Expression construct_member_access (string[] names) {
Vala.Expression expr = null;
for (var i = 0; names[i] != null; i++) {
......
......@@ -16,18 +16,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using GLib;
using Ide;
using Vala;
namespace Ide
{
public class ValaDiagnosticProvider: Ide.Object, Ide.DiagnosticProvider
{
public async Ide.Diagnostics? diagnose_async (Ide.File file,
GLib.Cancellable? cancellable)
throws GLib.Error
{
namespace Ide {
public class ValaDiagnosticProvider : Ide.Object, Ide.DiagnosticProvider {
public async Ide.Diagnostics? diagnose_async (Ide.File file, Cancellable? cancellable) throws GLib.Error {
var context = this.get_context ();
var service = (Ide.ValaService)context.get_service_typed (typeof (Ide.ValaService));
yield service.index.parse_file (file.file, context.unsaved_files, cancellable);
......
......@@ -16,25 +16,21 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using GLib;
using Vala;
namespace Ide
{
public class ValaDiagnostics: Vala.Report
{
public void clear ()
{
namespace Ide {
public class ValaDiagnostics : Vala.Report {
public void clear () {
this.errors = 0;
this.warnings = 0;
}
void add (Vala.SourceReference? source_reference,
string message,
Ide.DiagnosticSeverity severity)
{
if (source_reference == null)
Ide.DiagnosticSeverity severity) {
if (source_reference == null) {
return;
}
if (source_reference.file is Ide.ValaSourceFile) {
var file = (Ide.ValaSourceFile)source_reference.file;
......
......@@ -17,14 +17,10 @@
*/
using Gtk;
using Ide;
namespace Ide
{
public class ValaIndenter: Ide.Object, Ide.Indenter
{
public bool is_trigger (Gdk.EventKey evkey)
{
namespace Ide {
public class ValaIndenter : Ide.Object, Ide.Indenter {
public bool is_trigger (Gdk.EventKey evkey) {
switch (evkey.keyval) {
/* newline indents */
......@@ -45,8 +41,7 @@ namespace Ide
Gtk.TextIter begin,
Gtk.TextIter end,
out int cursor_offset,
Gdk.EventKey evkey)
{
Gdk.EventKey evkey) {
Gtk.SourceView source_view = (text_view as Gtk.SourceView);
bool was_newline = is_newline_keyval (evkey.keyval);
Gtk.TextIter copy = end;
......@@ -69,32 +64,33 @@ namespace Ide
}
}
if (was_newline)
if (was_newline) {
return indent_comment (text_view, copy);
}
}
if (is_newline_in_braces (copy)) {
string prefix = copy_indent (text_view, copy);
string indent;
if (source_view.insert_spaces_instead_of_tabs)
if (source_view.insert_spaces_instead_of_tabs) {
indent = " ";
else
} else {
indent = "\t";
}
cursor_offset = -prefix.length - 1;
return (prefix + indent + "\n" + prefix);
}
if (was_newline)
if (was_newline) {
return copy_indent (text_view, copy);
}
return null;
}
string? copy_indent (Gtk.TextView text_view,
Gtk.TextIter iter)
{
string? copy_indent (Gtk.TextView text_view, Gtk.TextIter iter) {
Gtk.TextIter begin = iter;
Gtk.TextIter end;
......@@ -108,48 +104,47 @@ namespace Ide
return begin.get_slice (end);
}
string get_line_text (Gtk.TextIter iter)
{
string get_line_text (Gtk.TextIter iter) {
Gtk.TextIter begin = iter;
Gtk.TextIter end = iter;
begin.set_line_offset (0);
if (!end.ends_line ())
if (!end.ends_line ()) {
end.forward_to_line_end ();
}
return begin.get_slice (end);
}
string? indent_comment (Gtk.TextView text_view,
Gtk.TextIter iter)
{
string? indent_comment (Gtk.TextView text_view, Gtk.TextIter iter) {
string line = get_line_text (iter).strip ();
/* continue with another single line comment */
if (line.has_prefix ("//"))
if (line.has_prefix ("//")) {
return copy_indent (text_view, iter) + "// ";
}
/* comment is closed, copy indent, possibly trimming extra space */
if (line.has_suffix ("*/")) {
if (line.has_prefix ("*")) {
var str = new GLib.StringBuilder (copy_indent (text_view, iter));
if (str.str.has_suffix (" "))
if (str.str.has_suffix (" ")) {
str.truncate (str.len - 1);
}
return str.str;
}
}
if (line.has_prefix ("/*") && !line.has_suffix ("*/"))
if (line.has_prefix ("/*") && !line.has_suffix ("*/")) {
return copy_indent (text_view, iter) + " * ";
else if (line.has_prefix ("*"))
} else if (line.has_prefix ("*")) {
return copy_indent (text_view, iter) + "* ";
}
return copy_indent (text_view, iter);
}
bool in_comment (Gtk.TextView text_view,
Gtk.TextIter iter)
{
bool in_comment (Gtk.TextView text_view, Gtk.TextIter iter) {
Gtk.SourceBuffer buffer = text_view.buffer as Gtk.SourceBuffer;
Gtk.TextIter copy = iter;
......@@ -158,8 +153,7 @@ namespace Ide
return buffer.iter_has_context_class (copy, "comment");
}
bool is_newline_keyval (uint keyval)
{
bool is_newline_keyval (uint keyval) {
switch (keyval) {
case Gdk.Key.Return:
case Gdk.Key.KP_Enter:
......@@ -170,8 +164,7 @@ namespace Ide
}
}
bool is_newline_in_braces (Gtk.TextIter iter)
{
bool is_newline_in_braces (Gtk.TextIter iter) {
Gtk.TextIter prev = iter;
Gtk.TextIter next = iter;
......
......@@ -24,23 +24,18 @@
* Ide.Service (via Ide.ValaService) to keep an index-per-project.
*/
using GLib;
using Gtk;
using Ide;
using Vala;
namespace Ide
{
public class ValaIndex: GLib.Object
{
namespace Ide {
public class ValaIndex : GLib.Object {
Vala.CodeContext code_context;
Vala.Parser parser;
HashMap<GLib.File,Ide.ValaSourceFile> source_files;
Ide.ValaDiagnostics report;
public ValaIndex ()
{
this.source_files = new HashMap<GLib.File,Ide.ValaSourceFile> (GLib.File.hash, (GLib.EqualFunc)GLib.File.equal);
public ValaIndex () {
this.source_files = new HashMap<GLib.File, Ide.ValaSourceFile> (GLib.File.hash, (GLib.EqualFunc)GLib.File.equal);
this.code_context = new Vala.CodeContext ();
......@@ -109,8 +104,7 @@ namespace Ide
Vala.CodeContext.pop ();
}
void add_file (GLib.File file)
{
void add_file (GLib.File file) {
var path = file.get_path ();
if (path == null)
return;
......@@ -121,14 +115,13 @@ namespace Ide
this.source_files [file] = source_file;
}
public async void add_files (ArrayList<GLib.File> files,
GLib.Cancellable? cancellable)
{
public async void add_files (ArrayList<GLib.File> files, GLib.Cancellable? cancellable) {
Ide.ThreadPool.push (Ide.ThreadPoolKind.COMPILER, () => {
lock (this.code_context) {
Vala.CodeContext.push (this.code_context);
foreach (var file in files)
foreach (var file in files) {
this.add_file (file);
}
Vala.CodeContext.pop ();
GLib.Idle.add(add_files.callback);
}
......@@ -139,9 +132,7 @@ namespace Ide
public async bool parse_file (GLib.File file,
Ide.UnsavedFiles? unsaved_files,
GLib.Cancellable? cancellable)
throws GLib.Error
{
GLib.Cancellable? cancellable) throws GLib.Error {
GLib.GenericArray<UnsavedFile>? unsaved_files_copy = null;
if (unsaved_files != null) {
......@@ -153,8 +144,9 @@ namespace Ide
lock (this.code_context) {
Vala.CodeContext.push (this.code_context);
if (!this.source_files.contains (file))
if (!this.source_files.contains (file)) {
this.add_file (file);
}
this.apply_unsaved_files (unsaved_files_copy);
this.reparse ();
......@@ -180,8 +172,7 @@ namespace Ide
Ide.ValaCompletionProvider provider,
GLib.Cancellable? cancellable,
out int result_line,
out int result_column)
{
out int result_column) {
var unsaved_files_copy = unsaved_files.to_array ();
var result = new Ide.CompletionResults (provider.query);
......@@ -212,9 +203,7 @@ namespace Ide
return result;
}
public async Ide.Diagnostics? get_diagnostics (GLib.File file,
GLib.Cancellable? cancellable = null)
{