def test_driver(): adb_file = GPS.File("main.adb") ads_file = GPS.File("main.ads") # Open ADB file, do modifications, check for diagnostics. adb_buffer = GPS.EditorBuffer.get(adb_file) location = GPS.EditorLocation(adb_buffer, 3, 16) adb_buffer.insert(location, " ") yield timeout(diagnosticTimeout) gps_assert(GPS.Locations.list_locations("Diagnostics", adb_file.path), [GPS.FileLocation(adb_file, 3, 17), "Missing ';'"], "Unexpected diagnostics (assertion 1)") # Save and close ADB file. adb_buffer.save() adb_buffer.close() # Reopen ADB file and check for diagnostics adb_buffer = GPS.EditorBuffer.get(adb_file) yield timeout(diagnosticTimeout) if diagnosticOnDidOpen: gps_assert(GPS.Locations.list_locations("Diagnostics", adb_file.path), [GPS.FileLocation(adb_file, 3, 17), "Missing ';'"], "Unexpected diagnostics (assertion 2)") # Undo changes, check for diagnostics location = GPS.EditorLocation(adb_buffer, 3, 16) adb_buffer.delete(location, location) yield timeout(diagnosticTimeout) gps_assert(GPS.Locations.list_locations("Diagnostics", adb_file.path), [], "Unexpected diagnostics (assertion 3)") # Create new file, fill it, save as ADS and check for diagnostics ads_buffer = GPS.EditorBuffer.get_new() location = ads_buffer.at(1, 1) ads_buffer.insert(location, "procedure Main") ads_buffer.save(False, ads_file) yield timeout(diagnosticTimeout) if diagnosticOnDidOpen: gps_assert(GPS.Locations.list_locations("Diagnostics", ads_file.path), [GPS.FileLocation(adb_file, 1, 16), "Missing ';'"], "Unexpected diagnostics (assertion 4)") # Fix error in ADS file and check for diagnostics location = ads_buffer.at(1, 16) ads_buffer.insert(location, ";") yield timeout(diagnosticOnDidOpen) gps_assert(GPS.Locations.list_locations("Diagnostics", adb_file.path), [], "Unexpected diagnostics (assertion 5)")
def process(self, start, end): """Called by Background_Highlighter""" if self.entity: Location_Highlighter.process(self, start, end) else: buffer = start.buffer() while start < end: # Get the string on the line end_loc = start.end_of_line() s = buffer.get_chars(start, end_loc) # byte-sequence s = s.decode("utf8") # make unicode-string s_len = len(s) # Find the tokens on the current line index = 0 visible_column = 0 while index < s_len: if s[index].isalpha() or s[index] == '_': end_index = index + 1 while end_index < s_len and (s[end_index].isalnum() or s[end_index] == '_'): end_index += 1 if s[index:end_index] == self.word: self.highlighted += 1 self.style.apply( start=GPS.EditorLocation( buffer, start.line(), start.column() + visible_column), end=GPS.EditorLocation( buffer, start.line(), start.column() + visible_column + len(self.word) - 1)) visible_column += end_index - index index = end_index else: visible_column += 1 if s[index] == '\t': visible_column = (visible_column + 8 - (visible_column % 8)) index += 1 start = end_loc + 1
def process(self, start, end): # overriding ed = start.buffer() s = GPS.FileLocation(ed.file(), start.line(), start.column()) e = GPS.FileLocation(ed.file(), end.line(), end.column()) for entity_name, ref in self._refs: if s <= ref <= e: u = entity_name.decode("utf-8").lower() s2 = ed.at(ref.line(), ref.column()) try: e2 = s2 + (len(u) - 1) except Exception: # An invalid location ? continue b = ed.get_chars(s2, e2).decode("utf-8").lower() if b == u: self.highlighted += 1 self.style.apply(s2, e2) elif self.context > 0: for c in range(1, self.context + 1): # Search after original xref line (same column) try: s2 = GPS.EditorLocation(ed, ref.line() + c, ref.column()) e2 = s2 + (len(u) - 1) b = ed.get_chars(s2, e2).decode("utf-8").lower() if b == u: self.highlighted += 1 self.style.apply(s2, e2) break # Search before original xref line s2 = GPS.EditorLocation(ed, ref.line() - c, ref.column()) e2 = s2 + (len(u) - 1) b = ed.get_chars(s2, e2).decode("utf-8").lower() if b == u: self.highlighted += 1 self.style.apply(s2, e2) break except Exception: # An invalid location ? continue
def mc_up(): ed = GPS.EditorBuffer.get() view = ed.current_view() loc = view.cursor() line = max(loc.line() - 1, 1) to = GPS.EditorLocation(ed, line, loc.column()) if cursor_absent(ed, to): if (loc.end_of_line().column() > 1): ed.add_cursor(loc) else: ed.delete_cursor(to) ed.get_cursors()[0].set_manual_sync() view.goto(ed.at(line, loc.column())) ed.set_cursors_auto_sync()
def test_driver(): GPS.Preference("LSP-Ada-Diagnostics").set(True) GPS.execute_action("Restart ada language server") yield hook('language_server_started') yield wait_tasks() f = GPS.File('to_be_called.adb') sf = GPS.File('second.adb') b = GPS.EditorBuffer.get(f) b.find_all_refs(b.at(1, 17), True) yield hook("language_server_response_processed") yield wait_tasks() gps_assert( GPS.Locations.list_locations( "References for To_Be_Called (to_be_called.adb:1)", sf.path), [GPS.FileLocation(sf, 1, 6), '[reference] with To_Be_Called;', GPS.FileLocation(sf, 4, 4), '[call] To_Be_Called;'], "wrong list of locations (1)") GPS.Locations.remove_category( "References for To_Be_Called (to_be_called.adb:1)") yield wait_tasks() # Reopen project GPS.execute_action('/File/Open Recent Projects/test.gpr') yield wait_tasks() b = GPS.EditorBuffer.get(f) b.find_all_refs(b.at(1, 17), True) yield hook("language_server_response_processed") yield wait_tasks() gps_assert( GPS.Locations.list_locations( "References for To_Be_Called (to_be_called.adb:1)", sf.path), [GPS.FileLocation(sf, 1, 6), '[reference] with To_Be_Called;', GPS.FileLocation(sf, 4, 4), '[call] To_Be_Called;'], "wrong list of locations (2)") l = GPS.EditorLocation(b, 1, 16) b.insert (l, " ") yield timeout(1000) gps_assert(GPS.Locations.list_locations("Diagnostics", f.path), [GPS.FileLocation(f, 1, 17), 'Invalid token, ignored', GPS.FileLocation(f, 1, 18), "Missing ';'"], "Unexpected diagnostics")
def center_line(): """ Center the current line on the screen. If a comment line then the text inside the comment is centered, the comment markers remain unchanged. """ buffer = GPS.EditorBuffer.get() location = buffer.current_view().cursor() initial = location.create_mark() with buffer.new_undo_group(): start = location.beginning_of_line() end = location.end_of_line() text = buffer.get_chars(start, end) if text[0:2] == "--" or text[0:2] == "//" or text[0:2] == "##": start = start + 2 if text[-3:] == "--\n" or text[-3:] == "//\n" or text[-3:] == "##\n": # Use right comment characters to center the text end = end - 3 text = buffer.get_chars(start, end).strip() spaces = end.column() - start.column() + 1 - len(text) before = spaces / 2 after = spaces / 2 if before + after != spaces: after = after + 1 buffer.delete(start, end) buffer.insert(start, ' ' * before + text + ' ' * after) else: # No right comment characters, use the highlight column to center # the text col = GPS.Preference("Src-Editor-Highlight-Column").get() text = buffer.get_chars(start, end).strip() spaces = int(col) - start.column() - len(text) before = spaces / 2 buffer.delete(start, end - 1) buffer.insert(start, ' ' * before + text) # Move to next line buffer.current_view().goto(GPS.EditorLocation (buffer, line=initial.location().forward_line( 1).line(), column=location.column()))
def with_col(loc, col): return GPS.EditorLocation(loc.buffer(), loc.line(), col)
def highlight(self, *args, **kwargs): """ Compute the current context, and perform the highlighting. :param args: ignored, so that this function can be used in callbacks :param kwargs: ignored, so that this function can be used in callbacks """ if self.highlight_entities is None: # preferences_changed has not yet been called return entity = None word = None context = GPS.current_context() start_loc = None end_loc = None location = None try: # Get the lcoation from the current editor instead of getting it # from the context. This is needed to highlight occurrences when # the context is not the current editor (e.g: Search view). buffer = GPS.EditorBuffer.get(open=False) context = GPS.current_context() location = buffer.current_view().cursor() except Exception: buffer = None # If we want to highlight based on the selection, look for it first if self.highlight_selection: if buffer: start_loc = buffer.selection_start() end_loc = buffer.selection_end() if start_loc != end_loc: end_loc = end_loc.forward_char(-1) word = buffer.get_chars(start_loc, end_loc).strip() word = word.decode("utf8") # make unicode-string # Attempt entity highlighting. # Do this only if there is indeed an entity under the cursor, # and, then, only if the semantic tree for this file is already # available: we do not want to block the UI waiting for this # computation. if not word and (self.highlight_entities and GPS.SemanticTree( context.file()).is_ready() and context.entity_name()): try: entity = context.entity(approximate_search_fallback=False) except Exception: entity = None # No entity found, highlight the current text if not entity and not word and location and buffer: location = GPS.EditorLocation(buffer, location.line(), location.column()) word, _, _ = location.get_word() # Exit if we are highlighting the word or the entity that we were # already highlighting. if (entity and self.entity and self.entity == entity) \ or (word and self.word and self.word == word): return self.stop_highlight() self.remove_highlight() if not buffer or (not entity and not word): self.entity = None self.word = None return # Do nothing when we have a reserved key word, since this might be # slow and in general does not bring useful information. lang_re = buffer.get_lang().keywords if lang_re and word and re.match(lang_re, word): return self.entity = entity self.word = word if self.current_buffer and (self.current_buffer != buffer): # We have just switched buffers: clear the highlighting on the # previous buffer self.stop_highlight(buffer=self.current_buffer) self.remove_highlight(buffer=self.current_buffer) if self.entity: self.set_style(self.styles["entity"]) else: self.set_style(self.styles["text"]) self.current_buffer = buffer self.start_highlight(buffer=buffer)