aboutsummaryrefslogtreecommitdiffstats
path: root/python/vhdl_langserver/document.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/vhdl_langserver/document.py')
-rw-r--r--python/vhdl_langserver/document.py93
1 files changed, 55 insertions, 38 deletions
diff --git a/python/vhdl_langserver/document.py b/python/vhdl_langserver/document.py
index 1b3cbf40b..82d11fe56 100644
--- a/python/vhdl_langserver/document.py
+++ b/python/vhdl_langserver/document.py
@@ -23,7 +23,7 @@ class Document(object):
# back to bytes using this encoding. And we hope the result would be
# the same as the file. Because VHDL uses the iso 8859-1 character
# set, we use the same encoding. The client should also use 8859-1.
- encoding = 'iso-8859-1'
+ encoding = "iso-8859-1"
initial_gap_size = 4096
@@ -40,11 +40,11 @@ class Document(object):
src_bytes = source.encode(Document.encoding, "replace")
src_len = len(src_bytes)
buf_len = src_len + Document.initial_gap_size
- fileid = name_table.Get_Identifier(filename.encode('utf-8'))
+ fileid = name_table.Get_Identifier(filename.encode("utf-8"))
if os.path.isabs(filename):
dirid = name_table.Null_Identifier
else:
- dirid = name_table.Get_Identifier(dirname.encode('utf-8'))
+ dirid = name_table.Get_Identifier(dirname.encode("utf-8"))
sfe = files_map.Reserve_Source_File(dirid, fileid, buf_len)
files_map_editor.Fill_Text(sfe, ctypes.c_char_p(src_bytes), src_len)
return sfe
@@ -52,42 +52,45 @@ class Document(object):
def reload(self, source):
"""Reload the source of a document. """
src_bytes = source.encode(Document.encoding, "replace")
- files_map_editor.Fill_Text(self._fe,
- ctypes.c_char_p(src_bytes), len(src_bytes))
+ files_map_editor.Fill_Text(self._fe, ctypes.c_char_p(src_bytes), len(src_bytes))
def __str__(self):
return str(self.uri)
def apply_change(self, change):
"""Apply a change to the document."""
- text = change['text']
- change_range = change.get('range')
+ text = change["text"]
+ change_range = change.get("range")
text_bytes = text.encode(Document.encoding, "replace")
if not change_range:
# The whole file has changed
raise AssertionError
- #if len(text_bytes) < thin.Files_Map.Get_Buffer_Length(self._fe):
+ # if len(text_bytes) < thin.Files_Map.Get_Buffer_Length(self._fe):
# xxxx_replace
- #else:
+ # else:
# xxxx_free
# xxxx_allocate
- #return
+ # return
- start_line = change_range['start']['line']
- start_col = change_range['start']['character']
- end_line = change_range['end']['line']
- end_col = change_range['end']['character']
+ start_line = change_range["start"]["line"]
+ start_col = change_range["start"]["character"]
+ end_line = change_range["end"]["line"]
+ end_col = change_range["end"]["character"]
status = files_map_editor.Replace_Text(
self._fe,
- start_line + 1, start_col,
- end_line + 1, end_col,
- ctypes.c_char_p(text_bytes), len(text_bytes))
+ start_line + 1,
+ start_col,
+ end_line + 1,
+ end_col,
+ ctypes.c_char_p(text_bytes),
+ len(text_bytes),
+ )
if status:
return
-
+
# Failed to replace text.
# Increase size
self.gap_size *= 2
@@ -101,9 +104,13 @@ class Document(object):
self._fe = new_sfe
status = files_map_editor.Replace_Text(
self._fe,
- start_line + 1, start_col,
- end_line + 1, end_col,
- ctypes.c_char_p(text_bytes), len(text_bytes))
+ start_line + 1,
+ start_col,
+ end_line + 1,
+ end_col,
+ ctypes.c_char_p(text_bytes),
+ len(text_bytes),
+ )
assert status
def check_document(self, text):
@@ -112,7 +119,8 @@ class Document(object):
text_bytes = text.encode(Document.encoding, "replace")
files_map_editor.Check_Buffer_Content(
- self._fe, ctypes.c_char_p(text_bytes), len(text_bytes))
+ self._fe, ctypes.c_char_p(text_bytes), len(text_bytes)
+ )
@staticmethod
def add_to_library(tree):
@@ -127,8 +135,10 @@ class Document(object):
next_unit = nodes.Get_Chain(unit)
nodes.Set_Chain(unit, nodes.Null_Iir)
lib_unit = nodes.Get_Library_Unit(unit)
- if (lib_unit != nodes.Null_Iir
- and nodes.Get_Identifier(unit) != name_table.Null_Identifier):
+ if (
+ lib_unit != nodes.Null_Iir
+ and nodes.Get_Identifier(unit) != name_table.Null_Identifier
+ ):
# Put the unit (only if it has a library unit) in the library.
libraries.Add_Design_Unit_Into_Library(unit, False)
tree = nodes.Get_Design_File(unit)
@@ -163,13 +173,13 @@ class Document(object):
def flatten_symbols(self, syms, parent):
res = []
for s in syms:
- s['location'] = {'uri': self.uri, 'range': s['range']}
- del s['range']
- s.pop('detail', None)
+ s["location"] = {"uri": self.uri, "range": s["range"]}
+ del s["range"]
+ s.pop("detail", None)
if parent is not None:
- s['containerName'] = parent
+ s["containerName"] = parent
res.append(s)
- children = s.pop('children', None)
+ children = s.pop("children", None)
if children is not None:
res.extend(self.flatten_symbols(children, s))
return res
@@ -178,20 +188,22 @@ class Document(object):
log.debug("document_symbols")
if self._tree == nodes.Null_Iir:
return []
- syms = symbols.get_symbols_chain(self._fe, nodes.Get_First_Design_Unit(self._tree))
+ syms = symbols.get_symbols_chain(
+ self._fe, nodes.Get_First_Design_Unit(self._tree)
+ )
return self.flatten_symbols(syms, None)
def position_to_location(self, position):
- pos = files_map.File_Line_To_Position(self._fe, position['line'] + 1)
- return files_map.File_Pos_To_Location(self._fe, pos) + position['character']
+ pos = files_map.File_Line_To_Position(self._fe, position["line"] + 1)
+ return files_map.File_Pos_To_Location(self._fe, pos) + position["character"]
def goto_definition(self, position):
loc = self.position_to_location(position)
return references.goto_definition(self._tree, loc)
def format_range(self, rng):
- first_line = rng['start']['line'] + 1
- last_line = rng['end']['line'] + (1 if rng['end']['character'] != 0 else 0)
+ first_line = rng["start"]["line"] + 1
+ last_line = rng["end"]["line"] + (1 if rng["end"]["character"] != 0 else 0)
if last_line < first_line:
return None
if self._tree == nodes.Null_Iir:
@@ -201,9 +213,14 @@ class Document(object):
buffer = formatters.Get_C_String(hand)
buf_len = formatters.Get_Length(hand)
newtext = buffer[:buf_len].decode(Document.encoding)
- res = [ {'range': {
- 'start': { 'line': first_line - 1, 'character': 0},
- 'end': { 'line': last_line, 'character': 0}},
- 'newText': newtext}]
+ res = [
+ {
+ "range": {
+ "start": {"line": first_line - 1, "character": 0},
+ "end": {"line": last_line, "character": 0},
+ },
+ "newText": newtext,
+ }
+ ]
formatters.Free_Handle(hand)
return res