Ejemplo n.º 1
0
    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: Completer = context.completer
        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
Ejemplo n.º 2
0
    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']))
Ejemplo n.º 3
0
 def to_completion_item(cls, completion: Completion,
                        params: TextDocumentPosition) -> CompletionItem:
     key = completion.text
     start_position = LanguageService._get_start_position(
         params.position, completion.start_position)
     text_range = Range(start=start_position, end=params.position)
     kind = DISPLAY_META_MAP.get(completion.display_meta,
                                 CompletionItemKind.Unit)
     completion_item = CompletionItem()
     completion_item.label = key
     completion_item.detail = completion.display
     completion_item.insert_text = key
     completion_item.kind = kind
     completion_item.text_edit = TextEdit.from_data(text_range, key)
     # Add a sort text to put keywords after all other items
     completion_item.sort_text = f'~{key}' if completion_item.kind == CompletionItemKind.Keyword else key
     return completion_item
Ejemplo n.º 4
0
 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))