def send_definition_using_connected_completions( self, request_context: RequestContext, scriptparseinfo: ScriptParseInfo, params: TextDocumentPosition, context: ConnectionContext) -> bool: if not context or not context.is_connected: return False definition_result: DefinitionResult = None completer: PGCompleter = context.pgcompleter completions: List[Completion] = completer.get_completions( scriptparseinfo.document, None) if completions: word_under_cursor = scriptparseinfo.document.get_word_under_cursor( ) matching_completion = next( completion for completion in completions if completion.display == word_under_cursor) if matching_completion: connection = self._connection_service.get_connection( params.text_document.uri, ConnectionType.QUERY) scripter_instance = scripter.Scripter(connection) object_metadata = ObjectMetadata( None, None, matching_completion.display_meta, matching_completion.display, matching_completion.schema) create_script = scripter_instance.script( ScriptOperation.CREATE, object_metadata) if create_script: with tempfile.NamedTemporaryFile( mode='wt', delete=False, encoding='utf-8', suffix='.sql', newline=None) as namedfile: namedfile.write(create_script) if namedfile.name: file_uri = "file:///" + namedfile.name.strip('/') location_in_script = Location( file_uri, Range(Position(0, 1), Position(1, 1))) definition_result = DefinitionResult( False, None, [ location_in_script, ]) request_context.send_response( definition_result.locations) return True if definition_result is None: request_context.send_response(DefinitionResult(True, '', [])) return False
def test_get_text_selection(self): """Text the workspace service's public get_text method when getting a selection of the text of a file""" # Set up the service with a file workspace_service = WorkspaceService() file_uri = 'untitled:Test_file' file_text = os.linesep.join(['line1', 'line 2 content', ' line 3 ']) workspace_service._workspace.open_file(file_uri, file_text) # Retrieve the full text of the file and make sure it matches selection_range = Range(Position(1, 1), Position(2, 4)) result_text = workspace_service.get_text(file_uri, selection_range) self.assertEqual(result_text, os.linesep.join(['ine 2 content', ' lin']))
def test_get_token_range(self): """Should return a range around the word""" # Given a string with some words words = ['create', 'table', 'T1'] # indexes: 0-5 7-11 13-14 line = ' '.join(words) start_col_to_word: dict = { 0: 0, 3: 0, 6: 0, # if on space between words, should select previous word 8: 1, 10: 1, 12: 1, # on space between words, should select previous 13: 2, # 'c' -> last indent 14: 2 } # When I lookup the word range for a given input for start, expected_word_index in start_col_to_word.items(): expected_word = words[expected_word_index] expected_line = 2 pos = Position.from_data(2, start) text_range: Range = TextUtilities.get_token_range(pos, line) # Then I expect the line value to always match the Positions line self.assertEqual(text_range.start.line, expected_line) self.assertEqual(text_range.end.line, expected_line) # ... and I expect the word to be the one covered by the indent actual_word = line[text_range.start.character:text_range.end. character] self.assertEqual( actual_word, expected_word, 'For start {0} expected "{1}" actual "{2}"'.format( start, expected_word, actual_word)) # noqa
def test_validate_position_valid(self): # Setup: Create a script file with a selection of test text sf = self._get_test_script_file() # If: I validate a valid position # Then: It should complete successfully sf.validate_position(Position.from_data(2, 1))
def test_validate_position_end_of_line(self): """Test that the column that would add a character to a line is treated as a valid column""" # Set up the script file script_file = self._get_test_script_file() # If I validate the column that would add a character to the end of a line # Then no error should be raised script_file.validate_position(Position.from_data(2, 4))
def test_validate_position_invalid_col(self): # Setup: Create a script file with a selection of test text sf = self._get_test_script_file() # If: I validate an invalid col for col in [-100, -1, 5, 400]: with self.assertRaises(ValueError): sf.validate_position(Position.from_data(2, col))
def test_validate_position_invalid_line(self): # Setup: Create a script file with a selection of test text sf = self._get_test_script_file() # If: I validate an invalid line for line in [-100, -1, 4, 400]: with self.assertRaises(ValueError): sf.validate_position(Position.from_data(line, 1))
def _get_start_position(cls, end: Position, start_index: int) -> Position: start_col = end.character + start_index if start_col < 0: # Should not happen - for now, just set to 0 and assume it's a mistake start_col = 0 return Position(end.line, start_col)
def to_range(self): """Convert the SelectionData object to a workspace service Range object""" return Range(Position(self.start_line, self.start_column), Position(self.end_line, self.end_column))