[gedit-code-assistance] Make more binding friendly
- From: Jesse van den Kieboom <jessevdk src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gedit-code-assistance] Make more binding friendly
- Date: Wed, 14 Mar 2012 18:05:49 +0000 (UTC)
commit 3f9f07633bec8859a4c57821ad1bccc02e1f248c
Author: Jesse van den Kieboom <jesse vandenkieboom epfl ch>
Date: Wed Mar 14 19:05:07 2012 +0100
Make more binding friendly
backends/c/gcp-c-document.vala | 77 +++++++++++++++++++++++------------
src/Makefile.am | 1 +
src/gcp-diagnostic-support.vala | 68 +++++++++++++++++++------------
src/gcp-diagnostic-tags.vala | 4 +-
src/gcp-document.vala | 47 ++++++++++++---------
src/gcp-semantic-value-support.vala | 13 +++++-
src/gcp-source-index.vala | 40 +++++++++---------
src/gcp-view.vala | 8 ++-
8 files changed, 158 insertions(+), 100 deletions(-)
---
diff --git a/backends/c/gcp-c-document.vala b/backends/c/gcp-c-document.vala
index f118628..e4276d7 100644
--- a/backends/c/gcp-c-document.vala
+++ b/backends/c/gcp-c-document.vala
@@ -60,21 +60,42 @@ class Document : Gcp.Document,
}
}
- public DiagnosticTags tags {get; set;}
+ private DiagnosticTags d_tags;
private TranslationUnit d_tu;
private SymbolBrowser d_symbols;
- private SourceIndex<Diagnostic> d_diagnostics;
- private SourceIndex<SemanticValue> d_semantics;
+
+ private SourceIndex d_diagnostics;
+ private Mutex d_diagnosticsLock;
+
+ private SourceIndex d_semantics;
+ private Mutex d_semanticsLock;
public Document(Gedit.Document document)
{
- base(document);
+ Object(document: document);
+ }
+ public void set_diagnostic_tags(DiagnosticTags tags)
+ {
+ d_tags = tags;
+ }
+
+ public DiagnosticTags get_diagnostic_tags()
+ {
+ return d_tags;
+ }
+
+ construct
+ {
d_tu = new TranslationUnit();
d_symbols = new SymbolBrowser();
- d_diagnostics = new SourceIndex<Diagnostic>();
- d_semantics = new SourceIndex<SemanticValue>();
+
+ d_diagnostics = new SourceIndex();
+ d_diagnosticsLock = new Mutex();
+
+ d_semantics = new SourceIndex();
+ d_semanticsLock = new Mutex();
d_tu.update.connect(on_tu_update);
}
@@ -87,20 +108,26 @@ class Document : Gcp.Document,
}
}
- public void with_diagnostics(DiagnosticSupport.WithDiagnosticsCallback callback)
+ public SourceIndex begin_diagnostics()
{
- lock(d_diagnostics)
- {
- callback(d_diagnostics);
- }
+ d_diagnosticsLock.lock();
+ return d_diagnostics;
}
- public void with_semantics(SemanticValueSupport.WithSemanticValueCallback callback)
+ public void end_diagnostics()
{
- lock(d_semantics)
- {
- callback(d_semantics);
- }
+ d_diagnosticsLock.unlock();
+ }
+
+ public SourceIndex begin_semantics()
+ {
+ d_semanticsLock.lock();
+ return d_semantics;
+ }
+
+ public void end_semantics()
+ {
+ d_semanticsLock.unlock();
}
public TranslationUnit translation_unit
@@ -121,7 +148,7 @@ class Document : Gcp.Document,
private void update_diagnostics(CX.TranslationUnit tu)
{
- SourceIndex<Diagnostic> ndiag = new SourceIndex<Diagnostic>();
+ SourceIndex ndiag = new SourceIndex();
Log.debug("New diagnostics: %u", tu.num_diagnostics);
@@ -188,15 +215,14 @@ class Document : Gcp.Document,
d.spelling.str()));
}
- lock(d_diagnostics)
- {
- d_diagnostics = ndiag;
- }
+ d_diagnosticsLock.lock();
+ d_diagnostics = ndiag;
+ d_diagnosticsLock.unlock();
}
private void update_semantics(CX.TranslationUnit tu)
{
- SourceIndex<SemanticValue> sems = new SourceIndex<SemanticValue>();
+ SourceIndex sems = new SourceIndex();
HashMap<CursorWrapper, SemanticValue> semmap;
@@ -234,10 +260,9 @@ class Document : Gcp.Document,
}
});
- lock(d_semantics)
- {
- d_semantics = sems;
- }
+ d_semanticsLock.lock();
+ d_semantics = sems;
+ d_semanticsLock.unlock();
}
private void on_tu_update()
diff --git a/src/Makefile.am b/src/Makefile.am
index 5c9759b..b2b5319 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -57,6 +57,7 @@ valapkgs = \
gedit-3.0 \
libpeas-1.0 \
gobject-introspection-1.0 \
+ posix \
config
INTROSPECTION_COMPILER_ARGS = --includedir $(GEDIT_GIR_DIR)
diff --git a/src/gcp-diagnostic-support.vala b/src/gcp-diagnostic-support.vala
index 0b52f3d..6134fc9 100644
--- a/src/gcp-diagnostic-support.vala
+++ b/src/gcp-diagnostic-support.vala
@@ -22,58 +22,74 @@ using Gee;
namespace Gcp
{
+public delegate void WithDiagnosticsCallback(SourceIndex diagnostics);
+
public interface DiagnosticSupport : Document
{
- public abstract DiagnosticTags tags { get; set; }
-
public signal void diagnostics_updated();
- public delegate void WithDiagnosticsCallback(SourceIndex<Diagnostic> diagnostics);
-
- public abstract void with_diagnostics(WithDiagnosticsCallback callback);
+ public abstract SourceIndex begin_diagnostics();
+ public abstract void end_diagnostics();
public Diagnostic[] find_at(SourceLocation location)
{
- ArrayList<Diagnostic> ret = new ArrayList<Diagnostic>();
+ Diagnostic[] ret = new Diagnostic[0];
+
+ var diagnostics = begin_diagnostics();
- with_diagnostics((diagnostics) => {
- foreach (Diagnostic d in diagnostics.find_at(location))
- {
- ret.add(d);
- }
- });
+ foreach (var d in diagnostics.find_at(location))
+ {
+ ret += (Diagnostic)d;
+ }
- ret.sort_with_data((CompareDataFunc)sort_on_severity);
+ end_diagnostics();
- return ret.to_array();
+ Posix.qsort(ret, ret.length, sizeof(Diagnostic), (Posix.compar_fn_t)sort_on_severity);
+
+ return ret;
}
- private int sort_on_severity(Diagnostic? a, Diagnostic? b)
+ private int sort_on_severity(void *a, void *b)
{
- if (a.severity == b.severity)
+ Diagnostic? da = (Diagnostic ?)a;
+ Diagnostic? db = (Diagnostic ?)b;
+
+ if (da.severity == db.severity)
{
return 0;
}
// Higer priorities last
- return a.severity < b.severity ? -1 : 1;
+ return da.severity < db.severity ? -1 : 1;
}
public Diagnostic[] find_at_line(int line)
{
- ArrayList<Diagnostic> ret = new ArrayList<Diagnostic>();
+ Diagnostic[] ret = new Diagnostic[0];
- with_diagnostics((diagnostics) => {
- foreach (Diagnostic d in diagnostics.find_at_line(line))
- {
- ret.add(d);
- }
- });
+ var diagnostics = begin_diagnostics();
- ret.sort_with_data((CompareDataFunc)sort_on_severity);
+ foreach (var d in diagnostics.find_at_line(line))
+ {
+ ret += (Diagnostic)d;
+ }
+
+ end_diagnostics();
+
+ Posix.qsort(ret, ret.length, sizeof(Diagnostic), (Posix.compar_fn_t)sort_on_severity);
- return ret.to_array();
+ return ret;
}
+
+ public void with_diagnostics(WithDiagnosticsCallback callback)
+ {
+ var d = begin_diagnostics();
+ callback(d);
+ end_diagnostics();
+ }
+
+ public abstract void set_diagnostic_tags(Gcp.DiagnosticTags tags);
+ public abstract Gcp.DiagnosticTags get_diagnostic_tags();
}
}
diff --git a/src/gcp-diagnostic-tags.vala b/src/gcp-diagnostic-tags.vala
index f9abc2f..ec2f6c4 100644
--- a/src/gcp-diagnostic-tags.vala
+++ b/src/gcp-diagnostic-tags.vala
@@ -30,7 +30,7 @@ namespace Gcp
private TextTag? d_locationTag;
private TextTag? d_fixitTag;
- public class DiagnosticTags
+ public class DiagnosticTags : Object
{
public DiagnosticTags(TextView view)
{
@@ -134,7 +134,7 @@ namespace Gcp
get { return d_fixitTag; }
}
- public TextTag? get(Diagnostic.Severity severity)
+ public new TextTag? get(Diagnostic.Severity severity)
{
switch (severity)
{
diff --git a/src/gcp-document.vala b/src/gcp-document.vala
index f1a75c5..acaa60e 100644
--- a/src/gcp-document.vala
+++ b/src/gcp-document.vala
@@ -24,7 +24,20 @@ namespace Gcp
public class Document : GLib.Object
{
+ public Gedit.Document document
+ {
+ get
+ {
+ return d_document;
+ }
+ construct
+ {
+ d_document = value;
+ }
+ }
+
private Gedit.Document d_document;
+
private bool d_untitled;
private bool d_modified;
private string? d_text;
@@ -59,10 +72,8 @@ public class Document : GLib.Object
}
}
- public Document(Gedit.Document document)
+ construct
{
- d_document = document;
-
d_untitled = d_document.is_untitled();
d_modified = false;
d_text = null;
@@ -157,7 +168,7 @@ public class Document : GLib.Object
{
DiagnosticSupport sup = this as DiagnosticSupport;
- TextTag? tag = sup.tags[diagnostic.severity];
+ TextTag? tag = sup.get_diagnostic_tags()[diagnostic.severity];
string? category = mark_category_for_severity(diagnostic.severity);
d_document.apply_tag(tag, start, end);
@@ -226,7 +237,7 @@ public class Document : GLib.Object
mark_diagnostic_range(diagnostic, start, end);
- d_document.apply_tag(sup.tags.location_tag, start, end);
+ d_document.apply_tag(sup.get_diagnostic_tags().location_tag, start, end);
}
for (uint i = 0; i < diagnostic.fixits.length; ++i)
@@ -235,7 +246,7 @@ public class Document : GLib.Object
if (source_range(r, out start, out end))
{
- d_document.apply_tag(sup.tags.fixit_tag, start, end);
+ d_document.apply_tag(sup.get_diagnostic_tags().fixit_tag, start, end);
}
}
}
@@ -247,18 +258,20 @@ public class Document : GLib.Object
d_document.get_bounds(out start, out end);
- d_document.remove_tag(diagnostic.tags.error_tag, start, end);
- d_document.remove_tag(diagnostic.tags.warning_tag, start, end);
- d_document.remove_tag(diagnostic.tags.info_tag, start, end);
- d_document.remove_tag(diagnostic.tags.location_tag, start, end);
- d_document.remove_tag(diagnostic.tags.fixit_tag, start, end);
+ var tags = diagnostic.get_diagnostic_tags();
+
+ d_document.remove_tag(tags.error_tag, start, end);
+ d_document.remove_tag(tags.warning_tag, start, end);
+ d_document.remove_tag(tags.info_tag, start, end);
+ d_document.remove_tag(tags.location_tag, start, end);
+ d_document.remove_tag(tags.fixit_tag, start, end);
remove_marks();
diagnostic.with_diagnostics((diagnostics) => {
- foreach (Diagnostic diag in diagnostics)
+ foreach (var diag in diagnostics)
{
- mark_diagnostic(diag);
+ mark_diagnostic((Diagnostic)diag);
}
});
}
@@ -374,14 +387,6 @@ public class Document : GLib.Object
}
}
- public Gedit.Document document
- {
- get
- {
- return d_document;
- }
- }
-
private void on_document_end_user_action()
{
if (d_modified)
diff --git a/src/gcp-semantic-value-support.vala b/src/gcp-semantic-value-support.vala
index 92c121b..f659f6b 100644
--- a/src/gcp-semantic-value-support.vala
+++ b/src/gcp-semantic-value-support.vala
@@ -1,11 +1,20 @@
namespace Gcp
{
+public delegate void WithSemanticValueCallback(SourceIndex diagnostics);
+
public interface SemanticValueSupport : Gcp.Document
{
- public delegate void WithSemanticValueCallback(SourceIndex<SemanticValue> diagnostics);
+ public void with_semantics(WithSemanticValueCallback callback)
+ {
+ var sems = begin_semantics();
+ callback(sems);
+
+ end_semantics();
+ }
- public abstract void with_semantics(WithSemanticValueCallback callback);
+ public abstract SourceIndex begin_semantics();
+ public abstract void end_semantics();
public signal void semantic_values_updated();
}
diff --git a/src/gcp-source-index.vala b/src/gcp-source-index.vala
index 0da9fd1..be15e6e 100644
--- a/src/gcp-source-index.vala
+++ b/src/gcp-source-index.vala
@@ -22,7 +22,7 @@ using Gee;
namespace Gcp
{
-public class SourceIndex<T> : Object
+public class SourceIndex : Object
{
public class Wrapper : Object
{
@@ -41,7 +41,7 @@ public class SourceIndex<T> : Object
}
}
- public class Iterator<T> : Object
+ public class Iterator : Object
{
private SequenceIter<Wrapper> d_iter;
private bool d_first;
@@ -66,9 +66,9 @@ public class SourceIndex<T> : Object
return !d_iter.is_end();
}
- public new T get()
+ public new Object get()
{
- return (T)d_iter.get().obj;
+ return d_iter.get().obj;
}
}
@@ -82,7 +82,7 @@ public class SourceIndex<T> : Object
private Sequence<Wrapper> d_index;
- public SourceIndex()
+ construct
{
#if VALA_0_14
d_index = new Sequence<Wrapper>();
@@ -124,7 +124,7 @@ public class SourceIndex<T> : Object
});
}
- public new T? get(int idx)
+ public new Object? get(int idx)
{
SequenceIter<Wrapper>? iter = d_index.get_iter_at_pos(idx);
@@ -133,7 +133,7 @@ public class SourceIndex<T> : Object
return null;
}
- return (T)iter.get().obj;
+ return iter.get().obj;
}
public int length
@@ -202,19 +202,19 @@ public class SourceIndex<T> : Object
});
}
- public T[] find_at_line(int line)
+ public Object[] find_at_line(int line)
{
return find_at_priv(new SourceLocation(null, line, 0), FindFlags.LINE_ONLY);
}
- public T[] find_at(SourceLocation location)
+ public Object[] find_at(SourceLocation location)
{
return find_at_priv(location, FindFlags.NONE);
}
- public T? find_inner_at(SourceLocation location)
+ public Object? find_inner_at(SourceLocation location)
{
- T[] ret = find_at_priv(location, FindFlags.INNER_MOST);
+ Object[] ret = find_at_priv(location, FindFlags.INNER_MOST);
if (ret.length == 0)
{
@@ -236,13 +236,13 @@ public class SourceIndex<T> : Object
(!lineonly && wrapper.range.contains_location(location));
}
- private T[] find_at_priv(SourceLocation location,
+ private Object[] find_at_priv(SourceLocation location,
FindFlags flags)
{
- LinkedList<T> ret = new LinkedList<T>();
+ LinkedList<Object> ret = new LinkedList<Object>();
SequenceIter<Wrapper> iter;
- HashMap<T, bool> uniq = new HashMap<T, bool>(direct_hash, direct_equal);
+ HashMap<Object, bool> uniq = new HashMap<Object, bool>(direct_hash, direct_equal);
iter = d_index.search(new Wrapper(location, location.range, 0), compare_func);
@@ -254,7 +254,7 @@ public class SourceIndex<T> : Object
if (find_at_condition(iter.get(), location, flags))
{
- return new T[] {iter.get().obj};
+ return new Object[] {iter.get().obj};
}
else if (!iter.get().encapsulated)
{
@@ -262,7 +262,7 @@ public class SourceIndex<T> : Object
}
}
- return new T[] {};
+ return new Object[] {};
}
// Go back to find ranges that encapsulate the location
@@ -273,7 +273,7 @@ public class SourceIndex<T> : Object
while (find_at_condition(prev.get(), location, flags) ||
prev.get().encapsulated)
{
- T val = (T)prev.get().obj;
+ Object val = (Object)prev.get().obj;
if (find_at_condition(prev.get(), location, flags) &&
!uniq.has_key(val))
@@ -296,7 +296,7 @@ public class SourceIndex<T> : Object
(find_at_condition(iter.get(), location, flags) ||
iter.get().encapsulated))
{
- T val = (T)iter.get().obj;
+ Object val = (Object)iter.get().obj;
if (find_at_condition(iter.get(), location, flags) && !uniq.has_key(val))
{
@@ -323,9 +323,9 @@ public class SourceIndex<T> : Object
return ra.compare_to(rb);
}
- public Iterator<T> iterator()
+ public Iterator iterator()
{
- return new Iterator<T>(d_index.get_begin_iter());
+ return new Iterator(d_index.get_begin_iter());
}
}
diff --git a/src/gcp-view.vala b/src/gcp-view.vala
index 0cb5fbd..f017087 100644
--- a/src/gcp-view.vala
+++ b/src/gcp-view.vala
@@ -421,7 +421,7 @@ class View : Object
{
MarkAttributes attr;
- diag.tags = d_tags;
+ diag.set_diagnostic_tags(d_tags);
diag.diagnostics_updated.connect(on_diagnostic_updated);
// Error
@@ -539,8 +539,10 @@ class View : Object
uint mid = d_scrollbarMarker.new_merge_id();
diagnostics.with_diagnostics((diagnostics) => {
- foreach (Diagnostic d in diagnostics)
+ foreach (var obj in diagnostics)
{
+ Diagnostic d = (Diagnostic)obj;
+
Gdk.RGBA color = colors[d.severity];
Gdk.RGBA mix = mixed[d.severity];
@@ -829,7 +831,7 @@ class View : Object
SemanticValue? ret = null;
sem.with_semantics((semantics) => {
- ret = semantics.find_inner_at(loc);
+ ret = (SemanticValue)semantics.find_inner_at(loc);
});
return ret;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]