Browse files

0.11.0

  • Loading branch information...
1 parent 4b9d02c commit d4c53c3edb2bf3a5d0a01b4456e722faa903d6c5 @nagaozen committed Jan 20, 2011
Showing with 240 additions and 18 deletions.
  1. +5 −18 README.md
  2. +40 −0 autocomplete/jsonproposals.py
  3. +144 −0 autocomplete/jsonprovider.py
  4. +51 −0 autocomplete/utils.py
View
23 README.md
@@ -2,21 +2,7 @@ gEdit Autocomplete
==================
gEdit Autocomplete suggests word completions based on the text in the active
-document. It first attempts to suggest compound words for a given word start,
-and only if such words do not exist it resorts to suggesting simple words.
-
-Compond words consist of simple words joined via concatenators. The inbuild
-concatenators are targeting c-family, pascal-family, urls and emails:
-
-* `.`
-* `:`
-* `->`
-* `::`
-* `://`
-* `@`
-
-This version uses `Return` instead of `Tab`, so it plays nice with the snippets
-plugin.
+document and in an additional language library file.
Demo
----
@@ -31,9 +17,8 @@ Installation
------------
1. Copy both `autocomplete.gedit-plugin` and `autocomplete` folder to `~/.gnome2/gedit/plugins`
2. Open gedit and click `Edit -> Preferences -> Plugins`
-3. Check the `Autocomplete-x.x.x` and hit `Close`
-4. That's it! Now you can start coding and watch the popup working. Hit `Return`
- to accept the completion.
+3. Check the `Autocomplete (gedit-json-completion)` and hit `Close`
+4. That's it! Now you can start coding and watch the popup working. Hit `Return` or `Tab` to accept the completion.
**NOTE:** Windows users should follow the instructions of <http://zend.lojcomm.com.br/entries/setting-autocomplete-to-work-in-windows-gedit/>
@@ -65,11 +50,13 @@ don't know what one was the first but this one is a product by:
- Initial version: Osmo Salomaa <http://users.tkk.fi/~otsaloma/gedit/>
- 0.9.6: Alin Avasilcutei <http://gedit-autocomp.sourceforge.net/>
- 0.9.x, 0.10.x: Fabio Nagao <http://zend.lojcomm.com.br/> and Vincent Petithory <http://blog.lunar-dev.net/>
+- 0.11.0: Fabio Nagao <http://zend.lojcomm.com.br/>
Changes
-------
> @2011-01-20
+> Releasing 0.11.0
> Adding 0.10.0 tag
> @2010-06-06
View
40 autocomplete/jsonproposals.py
@@ -0,0 +1,40 @@
+# -*- coding: utf-8 -*-
+
+# gEdit AutoComplete (gedit-json-completion)
+# Copyright (C) 2011 Fabio Zendhi Nagao
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program 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 General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+import gtksourceview2 as gsv
+import gobject
+
+class JSONProposal(gobject.GObject, gsv.CompletionProposal):
+ def __init__(self, name):
+ gobject.GObject.__init__(self)
+
+ self.name = name
+
+ def do_get_text(self):
+ return self.name
+
+ def do_get_label(self):
+ return self.name
+
+ def do_get_info(self):
+ # FIXME: gettext
+ return 'No extra info available'
+
+gobject.type_register(JSONProposal)
+
+# ex:ts=4:et:
View
144 autocomplete/jsonprovider.py
@@ -0,0 +1,144 @@
+# -*- coding: utf-8 -*-
+
+# gEdit AutoComplete (gedit-json-completion)
+# Copyright (C) 2011 Fabio Zendhi Nagao
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program 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 General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+import gtk
+import gedit
+
+from gettext import gettext as _
+
+import gobject
+import gtksourceview2 as gsv
+import json
+
+from jsonproposals import JSONProposal
+
+import re
+import os
+import utils
+
+class JSONProvider(gobject.GObject, gsv.CompletionProvider):
+ MARK_NAME = 'JSONProviderCompletionMark'
+
+ def __init__(self, plugin):
+ gobject.GObject.__init__(self)
+
+ self.mark = None
+ self._plugin = plugin
+
+ self._libs = {}
+ self._libs['global'] = self.get_language_library('global')
+
+ def move_mark(self, buf, start):
+ # TODO: see do_get_mark_iter
+ mark = buf.get_mark(self.MARK_NAME)
+
+ if not mark:
+ buf.create_mark(self.MARK_NAME, start, True)
+ else:
+ buf.move_mark(mark, start)
+
+ def get_proposals(self, code, lang, word):
+ proposals = []
+
+ # Get global static proposals
+ for proposal in self.get_filtered_proposals(self._libs['global']['statics'].split(' '), word):
+ proposals.append(JSONProposal(proposal))
+
+ if lang:
+ lib = self.get_language_library(lang.get_id())
+
+ # Get language static proposals
+ if 'statics' in lib:
+ for proposal in self.get_filtered_proposals(lib['statics'].split(' '), word):
+ proposals.append(JSONProposal(proposal))
+
+ # Get language dynamic proposals
+ if 'dynamic' in lib:
+ if 'identificators' in lib['dynamic']:
+ identificators = lib['dynamic']['identificators']
+ for identificator in identificators:
+ re_identificator = re.compile(r'%s'%identificator, re.UNICODE)
+ for m in re_identificator.finditer(code):
+ tokens = lib["dynamic"]["members"].get(m.group("class"))
+ if tokens:
+ tokens = tokens.split(' ')
+ tokens = [ "%s%s%s" % (m.group("instance"), lib["dynamic"]["tokenSeparator"], token) for token in tokens ]
+ tokens = self.get_filtered_proposals(tokens, word)
+
+ proposals.extend([ JSONProposal(token) for token in tokens ])
+ return proposals
+
+ def do_get_start_iter(self, context, proposal):
+ buf = context.get_iter().get_buffer()
+ mark = buf.get_mark(self.MARK_NAME)
+
+ if not mark:
+ return None
+
+ return buf.get_iter_at_mark(mark)
+
+ def do_populate(self, context):
+ start, word = utils.get_word(context.get_iter())
+ if not word:
+ context.add_proposals(self, [], True)
+ return
+
+ self.move_mark(context.get_iter().get_buffer(), start)
+
+ code = utils.get_document(start)
+ lang = start.get_buffer().get_language()
+
+ proposals = self.get_proposals(code, lang, word)
+ context.add_proposals(self, proposals, True)
+
+ def do_get_name(self):
+ return _('AutoComplete Library')
+
+ def get_priority(self):
+ return 42
+
+ def do_get_activation(self):
+ return gsv.COMPLETION_ACTIVATION_INTERACTIVE | gsv.COMPLETION_ACTIVATION_USER_REQUESTED
+
+ def do_activate_proposal(self, proposal, piter):
+ # TODO: implement
+ return False
+
+ def get_language_library(self, lang):
+ if lang in self._libs:
+ return self._libs[lang]
+ else:
+ path = os.path.join( self._plugin.get_install_dir(), 'autocomplete', 'lib', "%s.json"%(lang) )
+
+ if not os.path.isfile(path):
+ return {}
+
+ f = open(path, "rb")
+ d = json.load(f)
+ f.close()
+
+ self._libs[lang] = d
+ return d
+
+ def get_filtered_proposals(self, words, prefix):
+ proposals = [word for word in words if word.startswith(prefix) and word != prefix]
+ return proposals
+
+gobject.type_register(JSONProvider)
+
+# ex:ts=4:et:
View
51 autocomplete/utils.py
@@ -0,0 +1,51 @@
+# -*- coding: utf-8 -*-
+
+# gEdit AutoComplete (gedit-json-completion)
+# Copyright (C) 2011 Fabio Zendhi Nagao
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program 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 General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+def get_word(piter):
+ a = piter.copy()
+ b = piter.copy()
+
+ while True:
+ if a.starts_line():
+ break
+
+ a.backward_char()
+ ch = a.get_char()
+
+ if not (ch.isalnum() or ch == '_' or ch == ':' or ch == '.' or ch == '-' or ch == '>'):
+ a.forward_char()
+ break
+
+ word = a.get_visible_text(b)
+ return a, word
+
+def get_document(piter):
+ a = piter.copy()
+ b = piter.copy()
+
+ while True:
+ if not a.backward_char():
+ break
+
+ while True:
+ if not b.forward_char():
+ break
+
+ return a.get_visible_text(b)
+
+# ex:ts=4:et:

0 comments on commit d4c53c3

Please sign in to comment.