Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .nvim.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vim.opt.runtimepath:append(vim.uv.cwd())


40 changes: 31 additions & 9 deletions rplugin/python3/isort_nvim.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import logging
import os
import re
from pathlib import Path
from subprocess import PIPE, Popen

import neovim
import pynvim

ISORT_COMMAND = "isort"
ISORT_OPTIONS = [
Expand All @@ -20,13 +22,22 @@
"--remove-import",
]

def init_logging(nvim):
log_file = nvim.command_output('echo stdpath("data")')+'/isort.nvim.log'
logging.basicConfig(
filename=log_file,
level=logging.DEBUG,
format="%(asctime)s - %(levelname)s - %(message)s"
)

@neovim.plugin
class IsortNvim:
@pynvim.plugin
class IsortNvim(object):
def __init__(self, nvim):
self.nvim = nvim
init_logging(nvim)
logging.debug("plugin initialized")

@neovim.command(
@pynvim.command(
"Isort", nargs="*", range="%", complete="customlist,IsortCompletions"
)
def isort_command(self, args, range):
Expand All @@ -35,10 +46,14 @@ def isort_command(self, args, range):
output = self._isort(text, *args, cwd=Path(buffer.name).parent)
if text != output:
lines = re.split(r"\r\n?|\n", output)
buffer[range[0] - 1 : range[1]] = lines
buffer[range[0] - 1: range[1]] = lines

@neovim.command(
"IsortSync", nargs="*", range="%", complete="customlist,IsortCompletions", sync=True
@pynvim.command(
"IsortSync",
nargs="*",
range="%",
complete="customlist,IsortCompletions",
sync=True,
)
def isort_sync_command(self, args, range):
self.isort_command(args, range)
Expand All @@ -47,7 +62,7 @@ def error(self, msg):
self.nvim.err_write("[isort] {}\n".format(msg))

def _get_lines(self, buffer, range):
lines = buffer[range[0] - 1 : range[1]]
lines = buffer[range[0] - 1: range[1]]
return "\n".join(lines)

def _isort(self, text, *args, cwd=None):
Expand All @@ -57,7 +72,14 @@ def _isort(self, text, *args, cwd=None):
output, error = proc.communicate(input=text.encode())
return output.decode()

@neovim.function("IsortCompletions", sync=True)
@pynvim.function("IsortCompletions", sync=True)
def isort_completions(self, args):
arglead, cmdline, cursorpos, *_ = args
return [option for option in ISORT_OPTIONS if option.startswith(arglead)]


'''
nvim = pynvim.attach('socket', path = '/tmp/nvim.sock')
nvim.exec_lua('vim.fn.stdpath("data")')
nvim.command_output('echo stdpath("data")')
'''