示例#1
0
    def tsimport(self):
        self.reload()
        symbol = self.vim.call('expand', '<cword>')
        cursor = self.vim.current.window.cursor
        cursorPosition = {"line": cursor[0], "col": cursor[1] + 1}

        currentlyImportedItems = utils.getCurrentImports(
            client, self.relative_file())

        if symbol in currentlyImportedItems:
            self.printMsg("%s is already imported\n" % symbol)
            return

        results = utils.getImportCandidates(client, self.relative_file(),
                                            cursorPosition)

        # No imports
        if len(results) == 0:
            self.printMsg('No import candidates were found.')
            return

        # Only one
        if len(results) == 1:
            fixes = list(
                map(lambda changes: changes["textChanges"],
                    results[0]["changes"]))

        # More than one, need to choose
        else:
            changeDescriptions = map(lambda x: x["description"], results)
            candidates = "\n".join([
                "[%s]: %s" % (ix, change)
                for ix, change in enumerate(changeDescriptions)
            ])
            input = self.vim.call(
                'input',
                'nvim-ts: More than 1 candidate found, Select from the following options:\n%s\nplease choose one: '
                % candidates,
                '',
            )

            # Input has been canceled
            if not input:
                self.printError('Import canceled')
                return

            # Input is out of range
            if int(input) > (len(results) - 1):
                self.printError('Selection not valid')
                return

            # Value input is present
            else:
                fixes = list(
                    map(lambda changes: changes["textChanges"],
                        results[int(input)]["changes"]))

        # apply fixes
        self.applyImportChanges(fixes)
示例#2
0
    def tsimport(self):
        symbol = self.vim.call('expand', '<cword>')
        currentlyImportedItems, lastImportLine = utils.getCurrentImports(
            self._client, self.relative_file())

        if symbol in currentlyImportedItems:
            self.vim.out_write("nvim-ts: %s is already imported\n" % symbol)
            return

        results = utils.getImportCandidates(self._client, self.relative_file(),
                                            symbol)

        # No imports
        if len(results) == 0:
            self.printMsg('No import candidates were found.')
            return

        # Only one
        if len(results) == 1:
            importBlock = utils.createImportBlock(
                symbol,
                utils.getRelativeImportPath(self.relative_file(), results[0]),
                self.vim.vars["nvim_typescript#tsimport#template"])

        # More than one, need to choose
        else:
            candidates = "\n".join([
                "[%s]: %s" % (ix, result) for ix, result in enumerate(results)
            ])
            input = self.vim.call(
                'input',
                'nvim-ts: More than 1 candidate found, Select from the following options:\n%s\nplease choose one: '
                % candidates,
                '',
            )

            self.log(int(input))
            # Input has been canceled
            if not input:
                self.printError('Import canceled')
                return

            # Input is out of range
            if int(input) > (len(results) - 1):
                self.printError('Selection not valid')
                return

            # Value input is present
            else:
                importBlock = utils.createImportBlock(
                    symbol,
                    utils.getRelativeImportPath(self.relative_file(),
                                                results[int(input)]),
                    self.vim.vars["nvim_typescript#tsimport#template"])

        self.vim.current.buffer.append(importBlock, lastImportLine)
    def tsimport(self):
        self.reload()
        symbol = self.vim.call('expand', '<cword>')
        cursor = self.vim.current.window.cursor
        cursorPosition = {"line": cursor[0], "col": cursor[1] + 1}

        currentlyImportedItems = utils.getCurrentImports(self._client, self.relative_file())

        if symbol in currentlyImportedItems:
            self.printMsg("%s is already imported\n" % symbol)
            return

        results = utils.getImportCandidates(self._client, self.relative_file(), cursorPosition)

        # No imports
        if len(results) == 0:
            self.printMsg('No import candidates were found.')
            return

        # Only one
        if len(results) == 1:
            fixes = list(map(lambda changes: changes[
                         "textChanges"], results[0]["changes"]))

        # More than one, need to choose
        else:
            changeDescriptions = map(lambda x: x["description"], results)
            candidates = "\n".join(["[%s]: %s" % (ix, change)
                                    for ix, change in enumerate(changeDescriptions)])
            input = self.vim.call('input', 'nvim-ts: More than 1 candidate found, Select from the following options:\n%s\nplease choose one: ' % candidates, '',)

            # Input has been canceled
            if not input:
                self.printError('Import canceled')
                return

            # Input is out of range
            if int(input) > (len(results) - 1):
                self.printError('Selection not valid')
                return

            # Value input is present
            else:
                fixes = list(map(lambda changes: changes[
                             "textChanges"], results[int(input)]["changes"]))

        # apply fixes
        self.applyImportChanges(fixes)
示例#4
0
    def tsimport(self):
        symbol = self.vim.call('expand', '<cword>')
        currentlyImportedItems, lastImportLine = utils.getCurrentImports(
            self._client, self.relative_file())
        if symbol in currentlyImportedItems:
            self.vim.out_write("nvim-ts: %s is already imported\n" % symbol)
            return

        results = utils.getImportCandidates(self._client, self.relative_file(),
                                            symbol)
        if len(results) == 0:
            self.vim.out_write('nvim-ts: No import candidates were found.\n')
            return

        if len(results) == 1:
            importBlock = utils.createImportBlock(
                symbol,
                utils.getRelativeImportPath(self.relative_file(), results[0]),
                self.vim.vars["nvim_typescript#tsimport#template"])
        else:
            candidates = "\n".join([
                "[%s]: %s" % (ix, result) for ix, result in enumerate(results)
            ])
            input = self.vim.call(
                'input',
                'nvim-ts: More than 1 candidate found, Select from the following options:\n%s\n please choose one: '
                % candidates,
                '',
            )
            importBlock = utils.createImportBlock(
                symbol,
                utils.getRelativeImportPath(self.relative_file(),
                                            results[int(input)]),
                self.vim.vars["nvim_typescript#tsimport#template"])

        self.vim.current.buffer.append(importBlock, lastImportLine)
示例#5
0
    def tsimport(self):
        symbol = self.vim.call('expand', '<cword>')
        cursor = self.vim.current.window.cursor
        cursorPosition = {"line": cursor[0], "col": cursor[1]}

        currentlyImportedItems = utils.getCurrentImports(
            self._client, self.relative_file())

        if symbol in currentlyImportedItems:
            self.vim.out_write("nvim-ts: %s is already imported\n" % symbol)
            return

        results = utils.getImportCandidates(self._client, self.relative_file(),
                                            cursorPosition)

        # No imports
        if len(results) == 0:
            self.printMsg('No import candidates were found.')
            return

        # Only one
        if len(results) == 1:
            fixes = list(
                map(lambda changes: changes["textChanges"],
                    results[0]["changes"]))

        # More than one, need to choose
        else:
            changeDescriptions = map(lambda x: x["description"], results)
            candidates = "\n".join([
                "[%s]: %s" % (ix, change)
                for ix, change in enumerate(changeDescriptions)
            ])
            input = self.vim.call(
                'input',
                'nvim-ts: More than 1 candidate found, Select from the following options:\n%s\nplease choose one: '
                % candidates,
                '',
            )

            # Input has been canceled
            if not input:
                self.printError('Import canceled')
                return

            # Input is out of range
            if int(input) > (len(results) - 1):
                self.printError('Selection not valid')
                return

            # Value input is present
            else:
                fixes = list(
                    map(lambda changes: changes["textChanges"],
                        results[int(input)]["changes"]))

        # apply fixes
        for textChanges in fixes:
            for change in textChanges:
                changeLine = change['start']['line'] - 1
                changeOffset = change['start']['offset']
                newText = change['newText'].strip()
                if changeOffset == 1:
                    self.vim.current.buffer.append(newText, changeLine)
                else:
                    lineToChange = self.vim.current.buffer[changeLine]
                    modifiedLine = lineToChange[:changeOffset -
                                                1] + newText + lineToChange[
                                                    changeOffset - 1:]
                    self.vim.current.buffer[changeLine] = modifiedLine
示例#6
0
 def test(self):
     client = MagicMock()
     client.getWorkspaceSymbols = MagicMock(return_value=tsserverOutput)
     candidates = utils.getImportCandidates(client, currentFile, symbol)
     self.assertEqual(candidates, expected)