def goto_begin(): """Find the begin keyword of the current block""" # Get the buffer buf = GPS.EditorBuffer.get() # Get the position of the cursor line = GPS.Editor.cursor_get_line(str(buf.file())) column = GPS.Editor.cursor_get_column(str(buf.file())) # Get the libadalang tree sloc = libadalang.Sloc(line, column) analysis = buf.get_analysis_unit() if analysis.root: node = analysis.root.lookup(sloc) while node and not isinstance(node, libadalang.BodyNode): node = node.parent if node: # At this point we found the current Body, we need to go through # its children to find its begin keyword. for child in node.children: if isinstance(child, libadalang.DeclarativePart): loc = buf.at(int(child.sloc_range.end.line), int(node.sloc_range.end.column)) buf.current_view().goto(loc) break return
def show_current_location(self, line, column): """Highlight the given location in the tree and scroll to it""" if not self.unit: return self.line = line self.column = column if self.compact_mode: self.store.clear() self._add_node(None, self.unit.root) self.view.expand_all() else: # Clear all previous highlighting for j in self.highlighted_iters: self.store[j][COL_FOREGROUND] = self.default_fg self.highlighted_iters = [] lowest_found = self._traverse_and_highlight(None, line, column) # If we have finished iterating, scroll to the lowest found if lowest_found: self.view.scroll_to_cell(self.store.get_path(lowest_found), self.node_col, True, 0.5, 0.5) # Display the current token in the label self.token = self.unit.lookup_token(libadalang.Sloc(line, column)) if self.token: self.message_label.set_markup("Token: <b>{}</b> {}".format( self.token.kind, self.token.text.strip() if self.token.text else '')) else: self.message_label.set_text("")
def get_subp_decl(buf, line, column): """ Return the subprogram declaration node at line, column. Return None if none is found. """ unit = buf.get_analysis_unit() identifier_node = unit.root.lookup(lal.Sloc(line, column)) assert (isinstance(identifier_node, lal.Identifier)) decl = identifier_node.p_semantic_parent return decl
def cursorNode(self): file = self.ctx.get_from_file(self.vim.current.buffer.api.get_name(), reparse=True) (line, col) = self.vim.current.window.api.get_cursor() # Cursor is 0 based on columns, LAL is 1 based sloc = lal.Sloc(line, col + 1) return file.root.lookup(sloc)
def indent_for_line(line, buffer_text, unit, mmz_context=None): """ """ line_text = buffer_text[line - 1] start_col = 1 for i, c in enumerate(line_text, 1): start_col = i if not c.isspace(): break n = unit.root.lookup(lal.Sloc(line, start_col)) return indent_for_node(n, mmz_context)
def ada_get_indent_node(self, args): unit = self.current_unit() if not unit.root: return line = self.vim.eval('line(".")') col = self.vim.eval('col(".")') logger.info("In Ada get indent node") self.vim.command('echo "Loc {} {}"'.format(line, col)) current_node = unit.root.lookup(lal.Sloc(line, col)) logger.info("Current node = {}".format(current_node)) return current_node
def cycle_in_entity(): context = GPS.current_context() current_file = context.file() if not current_file or not current_file.language().lower() == "ada": return buf = GPS.EditorBuffer.get(current_file, open=False) if not buf: return location = context.location() line = location.line() column = location.column() unit = buf.get_analysis_unit() node = unit.root.lookup(libadalang.Sloc(line, column)) begin_line = None # Search for the enclosing block if node: for parent in node.parents(): if isinstance(parent, (libadalang.BaseSubpBody, libadalang.TaskBody, libadalang.EntryBody, libadalang.DeclBlock)): start_line = parent.sloc_range.start.line # We only need the start_column => everything should be # aligned dest_col = parent.sloc_range.start.column end_line = parent.sloc_range.end.line # Search for the declarative part (begin block) for child in parent.children: if isinstance(child, libadalang.DeclarativePart): begin_line = child.sloc_range.end.line break break else: return try: if begin_line and line < begin_line: dest_line = begin_line elif line < end_line: dest_line = end_line else: dest_line = start_line buf.current_view().goto(buf.at(int(dest_line), int(dest_col))) except NameError: # An exception can be raised if no parent was found (end_line, dest_col # can be not set) return
""" Test that AdaNode.lookup works properly. """ from __future__ import absolute_import, division, print_function import libadalang as lal ctx = lal.AnalysisContext() unit = ctx.get_from_file('foo.adb') for line, column in [(0, 0), (1, 1), (5, 80), (6, 16), (8, 12)]: sloc = lal.Sloc(line, column) n = unit.root.lookup(sloc) print('Lookup {}: {}'.format(sloc, n)) print('Done.')
import libadalang as lal ctx = lal.AnalysisContext(with_trivia=True) unit = ctx.get_from_file('lal-highlighters.adb') loc = lal.Sloc(1, 1) t = unit.lookup_token(loc) for i in range(40): print(t) t = t.next