Major completion provider overhaul; pluigin load and pattern improvements; css overhaul/cleanup; source view state modes added
This commit is contained in:
3
plugins/lsp_completer/__init__.py
Normal file
3
plugins/lsp_completer/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
"""
|
||||
Pligin Module
|
||||
"""
|
||||
3
plugins/lsp_completer/__main__.py
Normal file
3
plugins/lsp_completer/__main__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
"""
|
||||
Pligin Package
|
||||
"""
|
||||
7
plugins/lsp_completer/manifest.json
Normal file
7
plugins/lsp_completer/manifest.json
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"name": "LSP Completer",
|
||||
"author": "ITDominator",
|
||||
"version": "0.0.1",
|
||||
"support": "",
|
||||
"requests": {}
|
||||
}
|
||||
43
plugins/lsp_completer/plugin.py
Normal file
43
plugins/lsp_completer/plugin.py
Normal file
@@ -0,0 +1,43 @@
|
||||
# Python imports
|
||||
|
||||
# Lib imports
|
||||
import gi
|
||||
gi.require_version('Gtk', '3.0')
|
||||
from gi.repository import Gtk
|
||||
|
||||
# Application imports
|
||||
from libs.dto.base_event import BaseEvent
|
||||
from libs.event_factory import Event_Factory
|
||||
|
||||
from plugins.plugin_types import PluginCode
|
||||
|
||||
from .provider import Provider
|
||||
|
||||
|
||||
|
||||
class Plugin(PluginCode):
|
||||
def __init__(self):
|
||||
super(Plugin, self).__init__()
|
||||
|
||||
self.provider: Provider = None
|
||||
|
||||
|
||||
def _controller_message(self, event: BaseEvent):
|
||||
...
|
||||
|
||||
def load(self):
|
||||
self.provider = Provider()
|
||||
|
||||
event = Event_Factory.create_event(
|
||||
"register_provider",
|
||||
provider_name = "LSP Completer",
|
||||
provider = self.provider,
|
||||
language_ids = []
|
||||
)
|
||||
self.message_to("completion", event)
|
||||
|
||||
def run(self):
|
||||
...
|
||||
|
||||
def generate_plugin_element(self):
|
||||
...
|
||||
79
plugins/lsp_completer/provider.py
Normal file
79
plugins/lsp_completer/provider.py
Normal file
@@ -0,0 +1,79 @@
|
||||
# Python imports
|
||||
|
||||
# Lib imports
|
||||
import gi
|
||||
gi.require_version('GtkSource', '4')
|
||||
|
||||
from gi.repository import GtkSource
|
||||
from gi.repository import GObject
|
||||
|
||||
# Application imports
|
||||
from .provider_response_cache import ProviderResponseCache
|
||||
|
||||
|
||||
|
||||
class Provider(GObject.Object, GtkSource.CompletionProvider):
|
||||
"""
|
||||
This code is an LSP code completion plugin for Newton.
|
||||
# NOTE: Some code pulled/referenced from here --> https://github.com/isamert/gedi
|
||||
"""
|
||||
__gtype_name__ = 'LSPProvider'
|
||||
|
||||
def __init__(self):
|
||||
GObject.Object.__init__(self)
|
||||
|
||||
self.response_cache: ProviderResponseCache = ProviderResponseCache()
|
||||
|
||||
|
||||
def pre_populate(self, context):
|
||||
...
|
||||
|
||||
def do_get_name(self):
|
||||
return "LSP Code Completion"
|
||||
|
||||
def get_iter_correctly(self, context):
|
||||
return context.get_iter()[1] if isinstance(context.get_iter(), tuple) else context.get_iter()
|
||||
|
||||
def do_match(self, context):
|
||||
word = self.response_cache.get_word(context)
|
||||
if not word or len(word) < 2: return False
|
||||
|
||||
iter = self.get_iter_correctly(context)
|
||||
iter.backward_char()
|
||||
ch = iter.get_char()
|
||||
# NOTE: Look to re-add or apply supprting logic to use spaces
|
||||
# As is it slows down the editor in certain contexts...
|
||||
# if not (ch in ('_', '.', ' ') or ch.isalnum()):
|
||||
if not (ch in ('_', '.') or ch.isalnum()):
|
||||
return False
|
||||
|
||||
buffer = iter.get_buffer()
|
||||
if buffer.get_context_classes_at_iter(iter) != ['no-spell-check']:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def do_get_priority(self):
|
||||
return 5
|
||||
|
||||
def do_get_activation(self):
|
||||
""" The context for when a provider will show results """
|
||||
# return GtkSource.CompletionActivation.NONE
|
||||
return GtkSource.CompletionActivation.USER_REQUESTED
|
||||
# return GtkSource.CompletionActivation.INTERACTIVE
|
||||
|
||||
def do_populate(self, context):
|
||||
proposals = self.get_completion_filter(context)
|
||||
|
||||
context.add_proposals(self, proposals, True)
|
||||
|
||||
def get_completion_filter(self, context):
|
||||
proposals = [
|
||||
self.response_cache.create_completion_item(
|
||||
"LSP Class",
|
||||
"LSP Code",
|
||||
"A test LSP completion item..."
|
||||
)
|
||||
]
|
||||
|
||||
return proposals
|
||||
45
plugins/lsp_completer/provider_response_cache.py
Normal file
45
plugins/lsp_completer/provider_response_cache.py
Normal file
@@ -0,0 +1,45 @@
|
||||
# Python imports
|
||||
|
||||
# Lib imports
|
||||
import gi
|
||||
gi.require_version('GtkSource', '4')
|
||||
|
||||
from gi.repository import GtkSource
|
||||
|
||||
# Application imports
|
||||
from libs.event_factory import Code_Event_Types
|
||||
|
||||
from core.widgets.code.completion_providers.provider_response_cache_base import ProviderResponseCacheBase
|
||||
|
||||
|
||||
|
||||
class ProviderResponseCache(ProviderResponseCacheBase):
|
||||
def __init__(self):
|
||||
super(ProviderResponseCache, self).__init__()
|
||||
|
||||
|
||||
def process_file_load(self, event: Code_Event_Types.AddedNewFileEvent):
|
||||
...
|
||||
|
||||
def process_file_close(self, event: Code_Event_Types.RemovedFileEvent):
|
||||
...
|
||||
|
||||
def process_file_save(self, event: Code_Event_Types.SavedFileEvent):
|
||||
...
|
||||
|
||||
def process_file_change(self, event: Code_Event_Types.TextChangedEvent):
|
||||
...
|
||||
|
||||
def filter(self, word: str):
|
||||
...
|
||||
|
||||
def filter_with_context(self, context: GtkSource.CompletionContext):
|
||||
proposals = [
|
||||
self.create_completion_item(
|
||||
"LSP Class",
|
||||
"LSP Code",
|
||||
"A test LSP completion item..."
|
||||
)
|
||||
]
|
||||
|
||||
return proposals
|
||||
3
plugins/snippets_completer/__init__.py
Normal file
3
plugins/snippets_completer/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
"""
|
||||
Pligin Module
|
||||
"""
|
||||
3
plugins/snippets_completer/__main__.py
Normal file
3
plugins/snippets_completer/__main__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
"""
|
||||
Pligin Package
|
||||
"""
|
||||
3
plugins/snippets_completer/cson/__init__.py
Normal file
3
plugins/snippets_completer/cson/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from .parser import load, loads
|
||||
from .writer import dump, dumps
|
||||
from .speg import ParseError
|
||||
295
plugins/snippets_completer/cson/parser.py
Normal file
295
plugins/snippets_completer/cson/parser.py
Normal file
@@ -0,0 +1,295 @@
|
||||
from .speg import peg
|
||||
import re, sys
|
||||
|
||||
if sys.version_info[0] == 2:
|
||||
_chr = unichr
|
||||
else:
|
||||
_chr = chr
|
||||
|
||||
def load(fin):
|
||||
return loads(fin.read())
|
||||
|
||||
def loads(s):
|
||||
if isinstance(s, bytes):
|
||||
s = s.decode('utf-8')
|
||||
if s.startswith(u'\ufeff'):
|
||||
s = s[1:]
|
||||
return peg(s.replace('\r\n', '\n'), _p_root)
|
||||
|
||||
def _p_ws(p):
|
||||
p('[ \t]*')
|
||||
|
||||
def _p_nl(p):
|
||||
p(r'([ \t]*(?:#[^\n]*)?\r?\n)+')
|
||||
|
||||
def _p_ews(p):
|
||||
with p:
|
||||
p(_p_nl)
|
||||
p(_p_ws)
|
||||
|
||||
def _p_id(p):
|
||||
return p(r'[$a-zA-Z_][$0-9a-zA-Z_]*')
|
||||
|
||||
_escape_table = {
|
||||
'r': '\r',
|
||||
'n': '\n',
|
||||
't': '\t',
|
||||
'f': '\f',
|
||||
'b': '\b',
|
||||
}
|
||||
def _p_unescape(p):
|
||||
esc = p('\\\\(?:u[0-9a-fA-F]{4}|[^\n])')
|
||||
if esc[1] == 'u':
|
||||
return _chr(int(esc[2:], 16))
|
||||
return _escape_table.get(esc[1:], esc[1:])
|
||||
|
||||
_re_indent = re.compile(r'[ \t]*')
|
||||
def _p_block_str(p, c):
|
||||
p(r'{c}{c}{c}'.format(c=c))
|
||||
lines = [['']]
|
||||
with p:
|
||||
while True:
|
||||
s = p(r'(?:{c}(?!{c}{c})|[^{c}\\])*'.format(c=c))
|
||||
l = s.split('\n')
|
||||
lines[-1].append(l[0])
|
||||
lines.extend([x] for x in l[1:])
|
||||
if p(r'(?:\\\n[ \t]*)*'):
|
||||
continue
|
||||
p.commit()
|
||||
lines[-1].append(p(_p_unescape))
|
||||
p(r'{c}{c}{c}'.format(c=c))
|
||||
|
||||
lines = [''.join(l) for l in lines]
|
||||
strip_ws = len(lines) > 1
|
||||
if strip_ws and all(c in ' \t' for c in lines[-1]):
|
||||
lines.pop()
|
||||
|
||||
indent = None
|
||||
for line in lines[1:]:
|
||||
if not line:
|
||||
continue
|
||||
if indent is None:
|
||||
indent = _re_indent.match(line).group(0)
|
||||
continue
|
||||
for i, (c1, c2) in enumerate(zip(indent, line)):
|
||||
if c1 != c2:
|
||||
indent = indent[:i]
|
||||
break
|
||||
|
||||
ind_len = len(indent or '')
|
||||
if strip_ws and all(c in ' \t' for c in lines[0]):
|
||||
lines = [line[ind_len:] for line in lines[1:]]
|
||||
else:
|
||||
lines[1:] = [line[ind_len:] for line in lines[1:]]
|
||||
|
||||
return '\n'.join(lines)
|
||||
|
||||
_re_mstr_nl = re.compile(r'(?:[ \t]*\n)+[ \t]*')
|
||||
_re_mstr_trailing_nl = re.compile(_re_mstr_nl.pattern + r'\Z')
|
||||
def _p_multiline_str(p, c):
|
||||
p('{c}(?!{c}{c})(?:[ \t]*\n[ \t]*)?'.format(c=c))
|
||||
string_parts = []
|
||||
with p:
|
||||
while True:
|
||||
string_parts.append(p(r'[^{c}\\]*'.format(c=c)))
|
||||
if p(r'(?:\\\n[ \t]*)*'):
|
||||
string_parts.append('')
|
||||
continue
|
||||
p.commit()
|
||||
string_parts.append(p(_p_unescape))
|
||||
p(c)
|
||||
string_parts[-1] = _re_mstr_trailing_nl.sub('', string_parts[-1])
|
||||
string_parts[::2] = [_re_mstr_nl.sub(' ', part) for part in string_parts[::2]]
|
||||
return ''.join(string_parts)
|
||||
|
||||
def _p_string(p):
|
||||
with p:
|
||||
return p(_p_block_str, '"')
|
||||
with p:
|
||||
return p(_p_block_str, "'")
|
||||
with p:
|
||||
return p(_p_multiline_str, '"')
|
||||
return p(_p_multiline_str, "'")
|
||||
|
||||
def _p_array_value(p):
|
||||
with p:
|
||||
p(_p_nl)
|
||||
return p(_p_object)
|
||||
with p:
|
||||
p(_p_ws)
|
||||
return p(_p_line_object)
|
||||
p(_p_ews)
|
||||
return p(_p_simple_value)
|
||||
|
||||
def _p_key(p):
|
||||
with p:
|
||||
return p(_p_id)
|
||||
return p(_p_string)
|
||||
|
||||
def _p_flow_kv(p):
|
||||
k = p(_p_key)
|
||||
p(_p_ews)
|
||||
p(':')
|
||||
with p:
|
||||
p(_p_nl)
|
||||
return k, p(_p_object)
|
||||
with p:
|
||||
p(_p_ws)
|
||||
return k, p(_p_line_object)
|
||||
p(_p_ews)
|
||||
return k, p(_p_simple_value)
|
||||
|
||||
def _p_flow_obj_sep(p):
|
||||
with p:
|
||||
p(_p_ews)
|
||||
p(',')
|
||||
p(_p_ews)
|
||||
return
|
||||
|
||||
p(_p_nl)
|
||||
p(_p_ws)
|
||||
|
||||
def _p_simple_value(p):
|
||||
with p:
|
||||
p('null')
|
||||
return None
|
||||
|
||||
with p:
|
||||
p('false')
|
||||
return False
|
||||
with p:
|
||||
p('true')
|
||||
return True
|
||||
|
||||
with p:
|
||||
return int(p('0b[01]+')[2:], 2)
|
||||
with p:
|
||||
return int(p('0o[0-7]+')[2:], 8)
|
||||
with p:
|
||||
return int(p('0x[0-9a-fA-F]+')[2:], 16)
|
||||
with p:
|
||||
return float(p(r'-?(?:[1-9][0-9]*|0)?\.[0-9]+(?:[Ee][\+-]?[0-9]+)?|(?:[1-9][0-9]*|0)(?:\.[0-9]+)?[Ee][\+-]?[0-9]+'))
|
||||
with p:
|
||||
return int(p('-?[1-9][0-9]*|0'), 10)
|
||||
|
||||
with p:
|
||||
return p(_p_string)
|
||||
|
||||
with p:
|
||||
p(r'\[')
|
||||
r = []
|
||||
with p:
|
||||
p.set('I', '')
|
||||
r.append(p(_p_array_value))
|
||||
with p:
|
||||
while True:
|
||||
with p:
|
||||
p(_p_ews)
|
||||
p(',')
|
||||
rr = p(_p_array_value)
|
||||
if not p:
|
||||
p(_p_nl)
|
||||
with p:
|
||||
rr = p(_p_object)
|
||||
if not p:
|
||||
p(_p_ews)
|
||||
rr = p(_p_simple_value)
|
||||
r.append(rr)
|
||||
p.commit()
|
||||
with p:
|
||||
p(_p_ews)
|
||||
p(',')
|
||||
p(_p_ews)
|
||||
p(r'\]')
|
||||
return r
|
||||
|
||||
p(r'\{')
|
||||
|
||||
r = {}
|
||||
p(_p_ews)
|
||||
with p:
|
||||
p.set('I', '')
|
||||
k, v = p(_p_flow_kv)
|
||||
r[k] = v
|
||||
with p:
|
||||
while True:
|
||||
p(_p_flow_obj_sep)
|
||||
k, v = p(_p_flow_kv)
|
||||
r[k] = v
|
||||
p.commit()
|
||||
p(_p_ews)
|
||||
with p:
|
||||
p(',')
|
||||
p(_p_ews)
|
||||
p(r'\}')
|
||||
return r
|
||||
|
||||
def _p_line_kv(p):
|
||||
k = p(_p_key)
|
||||
p(_p_ws)
|
||||
p(':')
|
||||
p(_p_ws)
|
||||
with p:
|
||||
p(_p_nl)
|
||||
p(p.get('I'))
|
||||
return k, p(_p_indented_object)
|
||||
with p:
|
||||
return k, p(_p_line_object)
|
||||
with p:
|
||||
return k, p(_p_simple_value)
|
||||
p(_p_nl)
|
||||
p(p.get('I'))
|
||||
p('[ \t]')
|
||||
p(_p_ws)
|
||||
return k, p(_p_simple_value)
|
||||
|
||||
def _p_line_object(p):
|
||||
k, v = p(_p_line_kv)
|
||||
r = { k: v }
|
||||
with p:
|
||||
while True:
|
||||
p(_p_ws)
|
||||
p(',')
|
||||
p(_p_ws)
|
||||
k, v = p(_p_line_kv)
|
||||
r[k] = v # uniqueness
|
||||
p.commit()
|
||||
return r
|
||||
|
||||
def _p_object(p):
|
||||
p.set('I', p.get('I') + p('[ \t]*'))
|
||||
r = p(_p_line_object)
|
||||
with p:
|
||||
while True:
|
||||
p(_p_ws)
|
||||
with p:
|
||||
p(',')
|
||||
p(_p_nl)
|
||||
p(p.get('I'))
|
||||
rr = p(_p_line_object)
|
||||
r.update(rr) # unqueness
|
||||
p.commit()
|
||||
return r
|
||||
|
||||
def _p_indented_object(p):
|
||||
p.set('I', p.get('I') + p('[ \t]'))
|
||||
return p(_p_object)
|
||||
|
||||
def _p_root(p):
|
||||
with p:
|
||||
p(_p_nl)
|
||||
|
||||
with p:
|
||||
p.set('I', '')
|
||||
r = p(_p_object)
|
||||
p(_p_ws)
|
||||
with p:
|
||||
p(',')
|
||||
|
||||
if not p:
|
||||
p(_p_ws)
|
||||
r = p(_p_simple_value)
|
||||
|
||||
p(_p_ews)
|
||||
p(p.eof)
|
||||
return r
|
||||
1
plugins/snippets_completer/cson/speg/__init__.py
Normal file
1
plugins/snippets_completer/cson/speg/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .peg import peg, ParseError
|
||||
147
plugins/snippets_completer/cson/speg/peg.py
Normal file
147
plugins/snippets_completer/cson/speg/peg.py
Normal file
@@ -0,0 +1,147 @@
|
||||
import sys, re
|
||||
|
||||
class ParseError(Exception):
|
||||
def __init__(self, msg, text, offset, line, col):
|
||||
self.msg = msg
|
||||
self.text = text
|
||||
self.offset = offset
|
||||
self.line = line
|
||||
self.col = col
|
||||
super(ParseError, self).__init__(msg, offset, line, col)
|
||||
|
||||
if sys.version_info[0] == 2:
|
||||
_basestr = basestring
|
||||
else:
|
||||
_basestr = str
|
||||
|
||||
def peg(s, r):
|
||||
p = _Peg(s)
|
||||
try:
|
||||
return p(r)
|
||||
except _UnexpectedError as e:
|
||||
offset = max(p._errors)
|
||||
err = p._errors[offset]
|
||||
raise ParseError(err.msg, s, offset, err.line, err.col)
|
||||
|
||||
class _UnexpectedError(RuntimeError):
|
||||
def __init__(self, state, expr):
|
||||
self.state = state
|
||||
self.expr = expr
|
||||
|
||||
class _PegState:
|
||||
def __init__(self, pos, line, col):
|
||||
self.pos = pos
|
||||
self.line = line
|
||||
self.col = col
|
||||
self.vars = {}
|
||||
self.commited = False
|
||||
|
||||
class _PegError:
|
||||
def __init__(self, msg, line, col):
|
||||
self.msg = msg
|
||||
self.line = line
|
||||
self.col = col
|
||||
|
||||
class _Peg:
|
||||
def __init__(self, s):
|
||||
self._s = s
|
||||
self._states = [_PegState(0, 1, 1)]
|
||||
self._errors = {}
|
||||
self._re_cache = {}
|
||||
|
||||
def __call__(self, r, *args, **kw):
|
||||
if isinstance(r, _basestr):
|
||||
compiled = self._re_cache.get(r)
|
||||
if not compiled:
|
||||
compiled = re.compile(r)
|
||||
self._re_cache[r] = compiled
|
||||
st = self._states[-1]
|
||||
m = compiled.match(self._s[st.pos:])
|
||||
if not m:
|
||||
self.error(expr=r, err=kw.get('err'))
|
||||
|
||||
ms = m.group(0)
|
||||
st.pos += len(ms)
|
||||
nl_pos = ms.rfind('\n')
|
||||
if nl_pos < 0:
|
||||
st.col += len(ms)
|
||||
else:
|
||||
st.col = len(ms) - nl_pos
|
||||
st.line += ms[:nl_pos].count('\n') + 1
|
||||
return ms
|
||||
else:
|
||||
kw.pop('err', None)
|
||||
return r(self, *args, **kw)
|
||||
|
||||
def __repr__(self):
|
||||
pos = self._states[-1].pos
|
||||
vars = {}
|
||||
for st in self._states:
|
||||
vars.update(st.vars)
|
||||
return '_Peg(%r, %r)' % (self._s[:pos] + '*' + self._s[pos:], vars)
|
||||
|
||||
@staticmethod
|
||||
def eof(p):
|
||||
if p._states[-1].pos != len(p._s):
|
||||
p.error()
|
||||
|
||||
def error(self, err=None, expr=None):
|
||||
st = self._states[-1]
|
||||
if err is None:
|
||||
err = 'expected {!r}, found {!r}'.format(expr, self._s[st.pos:st.pos+4])
|
||||
self._errors[st.pos] = _PegError(err, st.line, st.col)
|
||||
raise _UnexpectedError(st, expr)
|
||||
|
||||
def get(self, key, default=None):
|
||||
for state in self._states[::-1]:
|
||||
if key in state.vars:
|
||||
return state.vars[key][0]
|
||||
return default
|
||||
|
||||
def set(self, key, value):
|
||||
self._states[-1].vars[key] = value, False
|
||||
|
||||
def set_global(self, key, value):
|
||||
self._states[-1].vars[key] = value, True
|
||||
|
||||
def opt(self, *args, **kw):
|
||||
with self:
|
||||
return self(*args, **kw)
|
||||
|
||||
def not_(self, s, *args, **kw):
|
||||
with self:
|
||||
self(s)
|
||||
self.error()
|
||||
|
||||
def __enter__(self):
|
||||
self._states[-1].committed = False
|
||||
self._states.append(_PegState(self._states[-1].pos, self._states[-1].line, self._states[-1].col))
|
||||
|
||||
def __exit__(self, type, value, traceback):
|
||||
if type is None:
|
||||
self.commit()
|
||||
self._states.pop()
|
||||
return type == _UnexpectedError
|
||||
|
||||
def commit(self):
|
||||
cur = self._states[-1]
|
||||
prev = self._states[-2]
|
||||
|
||||
for key in cur.vars:
|
||||
val, g = cur.vars[key]
|
||||
if not g:
|
||||
continue
|
||||
if key in prev.vars:
|
||||
prev.vars[key] = val, prev.vars[key][1]
|
||||
else:
|
||||
prev.vars[key] = val, True
|
||||
|
||||
prev.pos = cur.pos
|
||||
prev.line = cur.line
|
||||
prev.col = cur.col
|
||||
prev.committed = True
|
||||
|
||||
def __nonzero__(self):
|
||||
return self._states[-1].committed
|
||||
|
||||
__bool__ = __nonzero__
|
||||
191
plugins/snippets_completer/cson/writer.py
Normal file
191
plugins/snippets_completer/cson/writer.py
Normal file
@@ -0,0 +1,191 @@
|
||||
import re, json, sys
|
||||
|
||||
if sys.version_info[0] == 2:
|
||||
def _is_num(o):
|
||||
return isinstance(o, int) or isinstance(o, long) or isinstance(o, float)
|
||||
def _stringify(o):
|
||||
if isinstance(o, str):
|
||||
return unicode(o)
|
||||
if isinstance(o, unicode):
|
||||
return o
|
||||
return None
|
||||
else:
|
||||
def _is_num(o):
|
||||
return isinstance(o, int) or isinstance(o, float)
|
||||
def _stringify(o):
|
||||
if isinstance(o, bytes):
|
||||
return o.decode()
|
||||
if isinstance(o, str):
|
||||
return o
|
||||
return None
|
||||
|
||||
_id_re = re.compile(r'[$a-zA-Z_][$0-9a-zA-Z_]*\Z')
|
||||
|
||||
class CSONEncoder:
|
||||
def __init__(self, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False,
|
||||
indent=None, default=None):
|
||||
self._skipkeys = skipkeys
|
||||
self._ensure_ascii = ensure_ascii
|
||||
self._allow_nan = allow_nan
|
||||
self._sort_keys = sort_keys
|
||||
self._indent = ' ' * (indent or 4)
|
||||
self._default = default
|
||||
if check_circular:
|
||||
self._obj_stack = set()
|
||||
else:
|
||||
self._obj_stack = None
|
||||
|
||||
def _format_simple_val(self, o):
|
||||
if o is None:
|
||||
return 'null'
|
||||
if isinstance(o, bool):
|
||||
return 'true' if o else 'false'
|
||||
if _is_num(o):
|
||||
return str(o)
|
||||
s = _stringify(o)
|
||||
if s is not None:
|
||||
return self._escape_string(s)
|
||||
return None
|
||||
|
||||
def _escape_string(self, s):
|
||||
r = json.dumps(s, ensure_ascii=self._ensure_ascii)
|
||||
return u"'{}'".format(r[1:-1].replace("'", r"\'"))
|
||||
|
||||
def _escape_key(self, s):
|
||||
if s is None or isinstance(s, bool) or _is_num(s):
|
||||
s = str(s)
|
||||
s = _stringify(s)
|
||||
if s is None:
|
||||
if self._skipkeys:
|
||||
return None
|
||||
raise TypeError('keys must be a string')
|
||||
if not _id_re.match(s):
|
||||
return self._escape_string(s)
|
||||
return s
|
||||
|
||||
def _push_obj(self, o):
|
||||
if self._obj_stack is not None:
|
||||
if id(o) in self._obj_stack:
|
||||
raise ValueError('Circular reference detected')
|
||||
self._obj_stack.add(id(o))
|
||||
|
||||
def _pop_obj(self, o):
|
||||
if self._obj_stack is not None:
|
||||
self._obj_stack.remove(id(o))
|
||||
|
||||
def _encode(self, o, obj_val=False, indent='', force_flow=False):
|
||||
if isinstance(o, list):
|
||||
if not o:
|
||||
if obj_val:
|
||||
yield ' []\n'
|
||||
else:
|
||||
yield indent
|
||||
yield '[]\n'
|
||||
else:
|
||||
if obj_val:
|
||||
yield ' [\n'
|
||||
else:
|
||||
yield indent
|
||||
yield '[\n'
|
||||
indent = indent + self._indent
|
||||
self._push_obj(o)
|
||||
for v in o:
|
||||
for chunk in self._encode(v, obj_val=False, indent=indent, force_flow=True):
|
||||
yield chunk
|
||||
self._pop_obj(o)
|
||||
yield indent[:-len(self._indent)]
|
||||
yield ']\n'
|
||||
elif isinstance(o, dict):
|
||||
items = [(self._escape_key(k), v) for k, v in o.items()]
|
||||
if self._skipkeys:
|
||||
items = [(k, v) for k, v in items if k is not None]
|
||||
if self._sort_keys:
|
||||
items.sort()
|
||||
if force_flow or not items:
|
||||
if not items:
|
||||
if obj_val:
|
||||
yield ' {}\n'
|
||||
else:
|
||||
yield indent
|
||||
yield '{}\n'
|
||||
else:
|
||||
if obj_val:
|
||||
yield ' {\n'
|
||||
else:
|
||||
yield indent
|
||||
yield '{\n'
|
||||
indent = indent + self._indent
|
||||
self._push_obj(o)
|
||||
for k, v in items:
|
||||
yield indent
|
||||
yield k
|
||||
yield ':'
|
||||
for chunk in self._encode(v, obj_val=True, indent=indent + self._indent, force_flow=False):
|
||||
yield chunk
|
||||
self._pop_obj(o)
|
||||
yield indent[:-len(self._indent)]
|
||||
yield '}\n'
|
||||
else:
|
||||
if obj_val:
|
||||
yield '\n'
|
||||
self._push_obj(o)
|
||||
for k, v in items:
|
||||
yield indent
|
||||
yield k
|
||||
yield ':'
|
||||
for chunk in self._encode(v, obj_val=True, indent=indent + self._indent, force_flow=False):
|
||||
yield chunk
|
||||
self._pop_obj(o)
|
||||
else:
|
||||
v = self._format_simple_val(o)
|
||||
if v is None:
|
||||
self._push_obj(o)
|
||||
v = self.default(o)
|
||||
for chunk in self._encode(v, obj_val=obj_val, indent=indent, force_flow=force_flow):
|
||||
yield chunk
|
||||
self._pop_obj(o)
|
||||
else:
|
||||
if obj_val:
|
||||
yield ' '
|
||||
else:
|
||||
yield indent
|
||||
yield v
|
||||
yield '\n'
|
||||
|
||||
def iterencode(self, o):
|
||||
return self._encode(o)
|
||||
|
||||
def encode(self, o):
|
||||
return ''.join(self.iterencode(o))
|
||||
|
||||
def default(self, o):
|
||||
if self._default is None:
|
||||
raise TypeError('Cannot serialize an object of type {}'.format(type(o).__name__))
|
||||
return self._default(o)
|
||||
|
||||
def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None,
|
||||
indent=None, default=None, sort_keys=False, **kw):
|
||||
if indent is None and cls is None:
|
||||
return json.dump(obj, fp, skipkeys=skipkeys, ensure_ascii=ensure_ascii, check_circular=check_circular,
|
||||
allow_nan=allow_nan, default=default, sort_keys=sort_keys, separators=(',', ':'))
|
||||
|
||||
if cls is None:
|
||||
cls = CSONEncoder
|
||||
encoder = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii, check_circular=check_circular,
|
||||
allow_nan=allow_nan, sort_keys=sort_keys, indent=indent, default=default, **kw)
|
||||
|
||||
for chunk in encoder.iterencode(obj):
|
||||
fp.write(chunk)
|
||||
|
||||
def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None,
|
||||
default=None, sort_keys=False, **kw):
|
||||
if indent is None and cls is None:
|
||||
return json.dumps(obj, skipkeys=skipkeys, ensure_ascii=ensure_ascii, check_circular=check_circular,
|
||||
allow_nan=allow_nan, default=default, sort_keys=sort_keys, separators=(',', ':'))
|
||||
|
||||
if cls is None:
|
||||
cls = CSONEncoder
|
||||
encoder = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii, check_circular=check_circular,
|
||||
allow_nan=allow_nan, sort_keys=sort_keys, indent=indent, default=default, **kw)
|
||||
|
||||
return encoder.encode(obj)
|
||||
7
plugins/snippets_completer/manifest.json
Normal file
7
plugins/snippets_completer/manifest.json
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"name": "Snippets Completer",
|
||||
"author": "ITDominator",
|
||||
"version": "0.0.1",
|
||||
"support": "",
|
||||
"requests": {}
|
||||
}
|
||||
40
plugins/snippets_completer/plugin.py
Normal file
40
plugins/snippets_completer/plugin.py
Normal file
@@ -0,0 +1,40 @@
|
||||
# Python imports
|
||||
|
||||
# Lib imports
|
||||
import gi
|
||||
gi.require_version('Gtk', '3.0')
|
||||
from gi.repository import Gtk
|
||||
|
||||
# Application imports
|
||||
from libs.dto.base_event import BaseEvent
|
||||
from libs.event_factory import Event_Factory
|
||||
|
||||
from plugins.plugin_types import PluginCode
|
||||
|
||||
from .provider import Provider
|
||||
|
||||
|
||||
|
||||
class Plugin(PluginCode):
|
||||
def __init__(self):
|
||||
super(Plugin, self).__init__()
|
||||
|
||||
self.provider: Provider = None
|
||||
|
||||
|
||||
def _controller_message(self, event: BaseEvent):
|
||||
...
|
||||
|
||||
def load(self):
|
||||
self.provider = Provider()
|
||||
|
||||
event = Event_Factory.create_event(
|
||||
"register_provider",
|
||||
provider_name = "Snippets Completer",
|
||||
provider = self.provider,
|
||||
language_ids = []
|
||||
)
|
||||
self.message_to("completion", event)
|
||||
|
||||
def run(self):
|
||||
...
|
||||
63
plugins/snippets_completer/provider.py
Normal file
63
plugins/snippets_completer/provider.py
Normal file
@@ -0,0 +1,63 @@
|
||||
# Python imports
|
||||
import re
|
||||
|
||||
# Lib imports
|
||||
import gi
|
||||
gi.require_version('Gtk', '3.0')
|
||||
gi.require_version('GtkSource', '4')
|
||||
|
||||
from gi.repository import GObject
|
||||
from gi.repository import Gtk
|
||||
from gi.repository import GtkSource
|
||||
|
||||
# Application imports
|
||||
from .provider_response_cache import ProviderResponseCache
|
||||
|
||||
|
||||
|
||||
class Provider(GObject.GObject, GtkSource.CompletionProvider):
|
||||
"""
|
||||
This is a Snippits Completion Provider.
|
||||
# NOTE: used information from here --> https://warroom.rsmus.com/do-that-auto-complete/
|
||||
"""
|
||||
__gtype_name__ = 'SnippetsCompletionProvider'
|
||||
|
||||
def __init__(self):
|
||||
GObject.Object.__init__(self)
|
||||
|
||||
self.response_cache: ProviderResponseCache = ProviderResponseCache()
|
||||
|
||||
|
||||
def do_get_name(self):
|
||||
return 'Snippits Completion'
|
||||
|
||||
def do_match(self, context):
|
||||
word = self.response_cache.get_word(context)
|
||||
if not word or len(word) < 2: return False
|
||||
|
||||
return True
|
||||
|
||||
def do_get_priority(self):
|
||||
return 2
|
||||
|
||||
def do_get_activation(self):
|
||||
""" The context for when a provider will show results """
|
||||
# return GtkSource.CompletionActivation.NONE
|
||||
return GtkSource.CompletionActivation.USER_REQUESTED
|
||||
# return GtkSource.CompletionActivation.INTERACTIVE
|
||||
|
||||
def do_populate(self, context):
|
||||
word = self.response_cache.get_word(context)
|
||||
results = self.response_cache.filter(word)
|
||||
proposals = []
|
||||
|
||||
for entry in results:
|
||||
proposals.append(
|
||||
self.response_cache.create_completion_item(
|
||||
entry["label"],
|
||||
entry["text"],
|
||||
entry["info"]
|
||||
)
|
||||
)
|
||||
|
||||
context.add_proposals(self, proposals, True)
|
||||
63
plugins/snippets_completer/provider_response_cache.py
Normal file
63
plugins/snippets_completer/provider_response_cache.py
Normal file
@@ -0,0 +1,63 @@
|
||||
# Python imports
|
||||
from os import path
|
||||
|
||||
# Lib imports
|
||||
import gi
|
||||
gi.require_version('GtkSource', '4')
|
||||
|
||||
from gi.repository import GLib
|
||||
from gi.repository import GtkSource
|
||||
|
||||
# Application imports
|
||||
from libs.event_factory import Code_Event_Types
|
||||
|
||||
from core.widgets.code.completion_providers.provider_response_cache_base import ProviderResponseCacheBase
|
||||
|
||||
from . import cson
|
||||
|
||||
|
||||
|
||||
class ProviderResponseCache(ProviderResponseCacheBase):
|
||||
def __init__(self):
|
||||
super(ProviderResponseCache, self).__init__()
|
||||
|
||||
self.matchers: dict = {}
|
||||
|
||||
self.load_snippets()
|
||||
|
||||
|
||||
def load_snippets(self):
|
||||
fpath = path.join(path.dirname(path.realpath(__file__)), "snippets.cson")
|
||||
with open(fpath, 'rb') as f:
|
||||
self.snippets = cson.load(f)
|
||||
for group in self.snippets:
|
||||
self.snippets[group]
|
||||
for entry in self.snippets[group]:
|
||||
data = self.snippets[group][entry]
|
||||
self.matchers[ data["prefix"] ] = {
|
||||
"label": entry,
|
||||
"text": data["body"],
|
||||
"info": GLib.markup_escape_text( data["body"] )
|
||||
}
|
||||
|
||||
def process_file_load(self, event: Code_Event_Types.AddedNewFileEvent):
|
||||
...
|
||||
|
||||
def process_file_close(self, event: Code_Event_Types.RemovedFileEvent):
|
||||
...
|
||||
|
||||
def process_file_save(self, event: Code_Event_Types.SavedFileEvent):
|
||||
...
|
||||
|
||||
def process_file_change(self, event: Code_Event_Types.TextChangedEvent):
|
||||
...
|
||||
|
||||
def filter(self, word: str):
|
||||
response: list = []
|
||||
|
||||
for entry in self.matchers:
|
||||
if not word in entry: continue
|
||||
data = self.matchers[entry]
|
||||
response.append(data)
|
||||
|
||||
return response
|
||||
614
plugins/snippets_completer/snippets.cson
Normal file
614
plugins/snippets_completer/snippets.cson
Normal file
@@ -0,0 +1,614 @@
|
||||
# Your snippets
|
||||
#
|
||||
# Atom snippets allow you to enter a simple prefix in the editor and hit tab to
|
||||
# expand the prefix into a larger code block with templated values.
|
||||
#
|
||||
# You can create a new snippet in this file by typing "snip" and then hitting
|
||||
# tab.
|
||||
#
|
||||
# An example CoffeeScript snippet to expand log to console.log:
|
||||
#
|
||||
# '.source.coffee':
|
||||
# 'Console log':
|
||||
# 'prefix': 'log'
|
||||
# 'body': 'console.log $1'
|
||||
#
|
||||
# Each scope (e.g. '.source.coffee' above) can only be declared once.
|
||||
#
|
||||
# This file uses CoffeeScript Object Notation (CSON).
|
||||
# If you are unfamiliar with CSON, you can read more about it in the
|
||||
# Atom Flight Manual:
|
||||
# http://flight-manual.atom.io/using-atom/sections/basic-customization/#_cson
|
||||
|
||||
|
||||
### HTML SNIPPETS ###
|
||||
'.text.html.basic':
|
||||
|
||||
'HTML Template':
|
||||
'prefix': 'html'
|
||||
'body': """<!DOCTYPE html>
|
||||
<html lang="en" dir="ltr">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title></title>
|
||||
<link rel="shortcut icon" href="fave_icon.png">
|
||||
<link rel="stylesheet" href="resources/css/base.css">
|
||||
<link rel="stylesheet" href="resources/css/main.css">
|
||||
</head>
|
||||
<body>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<script src="resources/js/.js" charset="utf-8"></script>
|
||||
<script src="resources/js/.js" charset="utf-8"></script>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
'Canvas Tag':
|
||||
'prefix': 'canvas'
|
||||
'body': """<canvas id="canvas" width="800" height="600" style="border:1px solid #c3c3c3;"></canvas>"""
|
||||
|
||||
'Img Tag':
|
||||
'prefix': 'img'
|
||||
'body': """<img class="" src="" alt="" />"""
|
||||
|
||||
'Br Tag':
|
||||
'prefix': 'br'
|
||||
'body': """<br/>"""
|
||||
|
||||
'Hr Tag':
|
||||
'prefix': 'hr'
|
||||
'body': """<hr/>"""
|
||||
|
||||
'Server Side Events':
|
||||
'prefix': 'sse'
|
||||
'body': """// SSE events if supported
|
||||
if(typeof(EventSource) !== "undefined") {
|
||||
let source = new EventSource("resources/php/sse.php");
|
||||
source.onmessage = (event) => {
|
||||
if (event.data === "<yourDataStringToLookFor>") {
|
||||
// code here
|
||||
}
|
||||
};
|
||||
} else {
|
||||
console.log("SSE Not Supported In Browser...");
|
||||
}
|
||||
"""
|
||||
|
||||
'AJAX Template Function':
|
||||
'prefix': 'ajax template'
|
||||
'body': """const doAjax = async (actionPath, data) => {
|
||||
let xhttp = new XMLHttpRequest();
|
||||
|
||||
xhttp.onreadystatechange = function() {
|
||||
if (this.readyState === 4 && this.status === 200) {
|
||||
if (this.responseText != null) { // this.responseXML if getting XML fata
|
||||
handleReturnData(JSON.parse(this.responseText));
|
||||
} else {
|
||||
console.log("No content returned. Check the file path.");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
xhttp.open("POST", actionPath, true);
|
||||
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
|
||||
// Force return to be JSON NOTE: Use application/xml to force XML
|
||||
xhttp.overrideMimeType('application/json');
|
||||
xhttp.send(data);
|
||||
}
|
||||
"""
|
||||
|
||||
'CSS Message Colors':
|
||||
'prefix': 'css colors'
|
||||
'body': """.error { color: rgb(255, 0, 0); }
|
||||
.warning { color: rgb(255, 168, 0); }
|
||||
.success { color: rgb(136, 204, 39); }
|
||||
"""
|
||||
|
||||
|
||||
### JS SNIPPETS ###
|
||||
'.source.js':
|
||||
|
||||
'Server Side Events':
|
||||
'prefix': 'sse'
|
||||
'body': """// SSE events if supported
|
||||
if(typeof(EventSource) !== "undefined") {
|
||||
let source = new EventSource("resources/php/sse.php");
|
||||
source.onmessage = (event) => {
|
||||
if (event.data === "<yourDataStringToLookFor>") {
|
||||
// code here
|
||||
}
|
||||
};
|
||||
} else {
|
||||
console.log("SSE Not Supported In Browser...");
|
||||
}
|
||||
"""
|
||||
|
||||
'AJAX Template Function':
|
||||
'prefix': 'ajax template'
|
||||
'body': """const doAjax = async (actionPath, data) => {
|
||||
let xhttp = new XMLHttpRequest();
|
||||
|
||||
xhttp.onreadystatechange = function() {
|
||||
if (this.readyState === 4 && this.status === 200) {
|
||||
if (this.responseText != null) { // this.responseXML if getting XML fata
|
||||
handleReturnData(JSON.parse(this.responseText));
|
||||
} else {
|
||||
console.log("No content returned. Check the file path.");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
xhttp.open("POST", actionPath, true);
|
||||
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
|
||||
// Force return to be JSON NOTE: Use application/xml to force XML
|
||||
xhttp.overrideMimeType('application/json');
|
||||
xhttp.send(data);
|
||||
}
|
||||
"""
|
||||
|
||||
'SE6 Function':
|
||||
'prefix': 'function se6'
|
||||
'body': """const funcName = (arg = "") => {
|
||||
|
||||
}
|
||||
"""
|
||||
|
||||
### CSS SNIPPETS ###
|
||||
'.source.css':
|
||||
|
||||
'CSS Message Colors':
|
||||
'prefix': 'css colors'
|
||||
'body': """.error { color: rgb(255, 0, 0); }
|
||||
.warning { color: rgb(255, 168, 0); }
|
||||
.success { color: rgb(136, 204, 39); }
|
||||
"""
|
||||
|
||||
### PHP SNIPPETS ###
|
||||
'.text.html.php':
|
||||
|
||||
'SSE PHP':
|
||||
'prefix': 'sse php'
|
||||
'body': """<?php
|
||||
// Start the session
|
||||
session_start();
|
||||
|
||||
header('Content-Type: text/event-stream');
|
||||
header('Cache-Control: no-cache');
|
||||
|
||||
echo "data:dataToReturn\\\\n\\\\n";
|
||||
|
||||
flush();
|
||||
?>
|
||||
"""
|
||||
|
||||
'PHP Template':
|
||||
'prefix': 'php'
|
||||
'body': """<?php
|
||||
// Start the session
|
||||
session_start();
|
||||
|
||||
|
||||
// Determin action
|
||||
chdir("../../"); // Note: If in resources/php/
|
||||
if (isset($_POST['yourPostID'])) {
|
||||
// code here
|
||||
} else {
|
||||
$message = "Server: [Error] --> Illegal Access Method!";
|
||||
serverMessage("error", $message);
|
||||
}
|
||||
?>
|
||||
"""
|
||||
'HTML Template':
|
||||
'prefix': 'html'
|
||||
'body': """<!DOCTYPE html>
|
||||
<html lang="en" dir="ltr">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title></title>
|
||||
<link rel="shortcut icon" href="fave_icon.png">
|
||||
<link rel="stylesheet" href="resources/css/base.css">
|
||||
<link rel="stylesheet" href="resources/css/main.css">
|
||||
</head>
|
||||
<body>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<script src="resources/js/.js" charset="utf-8"></script>
|
||||
<script src="resources/js/.js" charset="utf-8"></script>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
|
||||
### BASH SNIPPETS ###
|
||||
'.source.shell':
|
||||
|
||||
'Bash or Shell Template':
|
||||
'prefix': 'bash template'
|
||||
'body': """#!/bin/bash
|
||||
|
||||
# . CONFIG.sh
|
||||
|
||||
# set -o xtrace ## To debug scripts
|
||||
# set -o errexit ## To exit on error
|
||||
# set -o errunset ## To exit if a variable is referenced but not set
|
||||
|
||||
|
||||
function main() {
|
||||
cd "$(dirname "$0")"
|
||||
echo "Working Dir: " $(pwd)
|
||||
|
||||
file="$1"
|
||||
if [ -z "${file}" ]; then
|
||||
echo "ERROR: No file argument supplied..."
|
||||
exit
|
||||
fi
|
||||
|
||||
if [[ -f "${file}" ]]; then
|
||||
echo "SUCCESS: The path and file exists!"
|
||||
else
|
||||
echo "ERROR: The path or file '${file}' does NOT exist..."
|
||||
fi
|
||||
}
|
||||
main "$@";
|
||||
"""
|
||||
|
||||
|
||||
'Bash or Shell Config':
|
||||
'prefix': 'bash config'
|
||||
'body': """#!/bin/bash
|
||||
|
||||
shopt -s expand_aliases
|
||||
|
||||
alias echo="echo -e"
|
||||
"""
|
||||
|
||||
|
||||
### PYTHON SNIPPETS ###
|
||||
'.source.python':
|
||||
|
||||
'Glade __main__ Class Template':
|
||||
'prefix': 'glade __main__ class'
|
||||
'body': """#!/usr/bin/python3
|
||||
|
||||
|
||||
# Python imports
|
||||
import argparse
|
||||
import faulthandler
|
||||
import traceback
|
||||
import signal
|
||||
from setproctitle import setproctitle
|
||||
|
||||
# Lib imports
|
||||
import gi
|
||||
gi.require_version('Gtk', '3.0')
|
||||
from gi.repository import Gtk
|
||||
from gi.repository import GLib
|
||||
|
||||
# Application imports
|
||||
from app import Application
|
||||
|
||||
|
||||
def run():
|
||||
try:
|
||||
setproctitle('<replace this>')
|
||||
faulthandler.enable() # For better debug info
|
||||
|
||||
GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, signal.SIGINT, Gtk.main_quit)
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
# Add long and short arguments
|
||||
parser.add_argument("--debug", "-d", default="false", help="Do extra console messaging.")
|
||||
parser.add_argument("--trace-debug", "-td", default="false", help="Disable saves, ignore IPC lock, do extra console messaging.")
|
||||
parser.add_argument("--no-plugins", "-np", default="false", help="Do not load plugins.")
|
||||
|
||||
parser.add_argument("--new-tab", "-t", default="", help="Open a file into new tab.")
|
||||
parser.add_argument("--new-window", "-w", default="", help="Open a file into a new window.")
|
||||
|
||||
# Read arguments (If any...)
|
||||
args, unknownargs = parser.parse_known_args()
|
||||
|
||||
main = Application(args, unknownargs)
|
||||
Gtk.main()
|
||||
except Exception as e:
|
||||
traceback.print_exc()
|
||||
quit()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
''' Set process title, get arguments, and create GTK main thread. '''
|
||||
run()
|
||||
|
||||
|
||||
"""
|
||||
|
||||
|
||||
'Glade __main__ Testing Template':
|
||||
'prefix': 'glade testing class'
|
||||
'body': """#!/usr/bin/python3
|
||||
|
||||
|
||||
# Python imports
|
||||
import traceback
|
||||
import faulthandler
|
||||
import signal
|
||||
|
||||
# Lib imports
|
||||
import gi
|
||||
gi.require_version('Gtk', '3.0')
|
||||
from gi.repository import Gtk
|
||||
from gi.repository import GLib
|
||||
|
||||
# Application imports
|
||||
|
||||
|
||||
app_name = "Gtk Quick Test"
|
||||
|
||||
|
||||
class Application(Gtk.ApplicationWindow):
|
||||
def __init__(self):
|
||||
super(Application, self).__init__()
|
||||
self._setup_styling()
|
||||
self._setup_signals()
|
||||
self._load_widgets()
|
||||
|
||||
self.add(Gtk.Box())
|
||||
|
||||
self.show_all()
|
||||
|
||||
|
||||
def _setup_styling(self):
|
||||
self.set_default_size(1670, 830)
|
||||
self.set_title(f"{app_name}")
|
||||
# self.set_icon_from_file( settings.get_window_icon() )
|
||||
self.set_gravity(5) # 5 = CENTER
|
||||
self.set_position(1) # 1 = CENTER, 4 = CENTER_ALWAYS
|
||||
|
||||
def _setup_signals(self):
|
||||
self.connect("delete-event", Gtk.main_quit)
|
||||
|
||||
|
||||
def _load_widgets(self):
|
||||
...
|
||||
|
||||
|
||||
|
||||
|
||||
def run():
|
||||
try:
|
||||
faulthandler.enable() # For better debug info
|
||||
GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, signal.SIGINT, Gtk.main_quit)
|
||||
|
||||
main = Application()
|
||||
Gtk.main()
|
||||
except Exception as e:
|
||||
traceback.print_exc()
|
||||
quit()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
''' Set process title, get arguments, and create GTK main thread. '''
|
||||
run()
|
||||
|
||||
|
||||
"""
|
||||
|
||||
|
||||
'Glade _init_ Class Template':
|
||||
'prefix': 'glade __init__ class'
|
||||
'body': """# Python imports
|
||||
import inspect
|
||||
|
||||
|
||||
# Lib imports
|
||||
|
||||
|
||||
# Application imports
|
||||
from utils import Settings
|
||||
from signal_classes import CrossClassSignals
|
||||
|
||||
|
||||
class Main:
|
||||
def __init__(self, args):
|
||||
settings = Settings()
|
||||
builder = settings.returnBuilder()
|
||||
|
||||
# Gets the methods from the classes and sets to handler.
|
||||
# Then, builder connects to any signals it needs.
|
||||
classes = [CrossClassSignals(settings)]
|
||||
|
||||
handlers = {}
|
||||
for c in classes:
|
||||
methods = inspect.getmembers(c, predicate=inspect.ismethod)
|
||||
handlers.update(methods)
|
||||
|
||||
builder.connect_signals(handlers)
|
||||
window = settings.createWindow()
|
||||
window.show()
|
||||
|
||||
"""
|
||||
|
||||
'Class Method':
|
||||
'prefix': 'def1'
|
||||
'body': """
|
||||
def fname(self):
|
||||
...
|
||||
"""
|
||||
|
||||
'Gtk Class Method':
|
||||
'prefix': 'def2'
|
||||
'body': """
|
||||
def fname(self, widget, eve):
|
||||
...
|
||||
"""
|
||||
|
||||
|
||||
'Python Glade Settings Template':
|
||||
'prefix': 'glade settings class'
|
||||
'body': """# Python imports
|
||||
import os
|
||||
|
||||
# Lib imports
|
||||
import gi, cairo
|
||||
gi.require_version('Gtk', '3.0')
|
||||
gi.require_version('Gdk', '3.0')
|
||||
|
||||
from gi.repository import Gtk
|
||||
from gi.repository import Gdk
|
||||
|
||||
|
||||
# Application imports
|
||||
|
||||
|
||||
class Settings:
|
||||
def __init__(self):
|
||||
self.SCRIPT_PTH = os.path.dirname(os.path.realpath(__file__)) + "/"
|
||||
self.builder = Gtk.Builder()
|
||||
self.builder.add_from_file(self.SCRIPT_PTH + "../resources/Main_Window.glade")
|
||||
|
||||
# 'Filters'
|
||||
self.office = ('.doc', '.docx', '.xls', '.xlsx', '.xlt', '.xltx', '.xlm',
|
||||
'.ppt', 'pptx', '.pps', '.ppsx', '.odt', '.rtf')
|
||||
self.vids = ('.mkv', '.avi', '.flv', '.mov', '.m4v', '.mpg', '.wmv',
|
||||
'.mpeg', '.mp4', '.webm')
|
||||
self.txt = ('.txt', '.text', '.sh', '.cfg', '.conf')
|
||||
self.music = ('.psf', '.mp3', '.ogg' , '.flac')
|
||||
self.images = ('.png', '.jpg', '.jpeg', '.gif')
|
||||
self.pdf = ('.pdf')
|
||||
|
||||
|
||||
def createWindow(self):
|
||||
# Get window and connect signals
|
||||
window = self.builder.get_object("Main_Window")
|
||||
window.connect("delete-event", gtk.main_quit)
|
||||
self.setWindowData(window, False)
|
||||
return window
|
||||
|
||||
def setWindowData(self, window, paintable):
|
||||
screen = window.get_screen()
|
||||
visual = screen.get_rgba_visual()
|
||||
|
||||
if visual != None and screen.is_composited():
|
||||
window.set_visual(visual)
|
||||
|
||||
# bind css file
|
||||
cssProvider = gtk.CssProvider()
|
||||
cssProvider.load_from_path(self.SCRIPT_PTH + '../resources/stylesheet.css')
|
||||
screen = Gdk.Screen.get_default()
|
||||
styleContext = Gtk.StyleContext()
|
||||
styleContext.add_provider_for_screen(screen, cssProvider, gtk.STYLE_PROVIDER_PRIORITY_USER)
|
||||
|
||||
window.set_app_paintable(paintable)
|
||||
if paintable:
|
||||
window.connect("draw", self.area_draw)
|
||||
|
||||
def getMonitorData(self):
|
||||
screen = self.builder.get_object("Main_Window").get_screen()
|
||||
monitors = []
|
||||
for m in range(screen.get_n_monitors()):
|
||||
monitors.append(screen.get_monitor_geometry(m))
|
||||
|
||||
for monitor in monitors:
|
||||
print(str(monitor.width) + "x" + str(monitor.height) + "+" + str(monitor.x) + "+" + str(monitor.y))
|
||||
|
||||
return monitors
|
||||
|
||||
def area_draw(self, widget, cr):
|
||||
cr.set_source_rgba(0, 0, 0, 0.54)
|
||||
cr.set_operator(cairo.OPERATOR_SOURCE)
|
||||
cr.paint()
|
||||
cr.set_operator(cairo.OPERATOR_OVER)
|
||||
|
||||
|
||||
def returnBuilder(self): return self.builder
|
||||
|
||||
# Filter returns
|
||||
def returnOfficeFilter(self): return self.office
|
||||
def returnVidsFilter(self): return self.vids
|
||||
def returnTextFilter(self): return self.txt
|
||||
def returnMusicFilter(self): return self.music
|
||||
def returnImagesFilter(self): return self.images
|
||||
def returnPdfFilter(self): return self.pdf
|
||||
|
||||
"""
|
||||
|
||||
'Python Glade CrossClassSignals Template':
|
||||
'prefix': 'glade crossClassSignals class'
|
||||
'body': """# Python imports
|
||||
import threading
|
||||
import subprocess
|
||||
import os
|
||||
|
||||
# Lib imports
|
||||
|
||||
# Application imports
|
||||
|
||||
|
||||
def threaded(fn):
|
||||
def wrapper(*args, **kwargs):
|
||||
threading.Thread(target=fn, args=args, kwargs=kwargs).start()
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
class CrossClassSignals:
|
||||
def __init__(self, settings):
|
||||
self.settings = settings
|
||||
self.builder = self.settings.returnBuilder()
|
||||
|
||||
|
||||
def getClipboardData(self):
|
||||
proc = subprocess.Popen(['xclip','-selection', 'clipboard', '-o'], stdout=subprocess.PIPE)
|
||||
retcode = proc.wait()
|
||||
data = proc.stdout.read()
|
||||
return data.decode("utf-8").strip()
|
||||
|
||||
def setClipboardData(self, data):
|
||||
proc = subprocess.Popen(['xclip','-selection','clipboard'], stdin=subprocess.PIPE)
|
||||
proc.stdin.write(data)
|
||||
proc.stdin.close()
|
||||
retcode = proc.wait()
|
||||
|
||||
"""
|
||||
|
||||
|
||||
'Python Glade Generic Template':
|
||||
'prefix': 'glade generic class'
|
||||
'body': """# Python imports
|
||||
|
||||
# Lib imports
|
||||
|
||||
# Application imports
|
||||
|
||||
|
||||
class GenericClass:
|
||||
def __init__(self):
|
||||
super(GenericClass, self).__init__()
|
||||
|
||||
self._setup_styling()
|
||||
self._setup_signals()
|
||||
self._subscribe_to_events()
|
||||
self._load_widgets()
|
||||
|
||||
|
||||
def _setup_styling(self):
|
||||
...
|
||||
|
||||
def _setup_signals(self):
|
||||
...
|
||||
|
||||
def _subscribe_to_events(self):
|
||||
event_system.subscribe("handle_file_from_ipc", self.handle_file_from_ipc)
|
||||
|
||||
def _load_widgets(self):
|
||||
...
|
||||
|
||||
"""
|
||||
Reference in New Issue
Block a user