Skip to content

Commit

Permalink
fixed version issues except for the Windows ones
Browse files Browse the repository at this point in the history
  • Loading branch information
James Brownlee authored and olegklimov committed Mar 3, 2024
1 parent c166dd0 commit 8071dc0
Show file tree
Hide file tree
Showing 11 changed files with 216 additions and 56 deletions.
1 change: 1 addition & 0 deletions Default.sublime-keymap
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[
{ "keys": ["tab"], "command": "refact_accept_completion", "context": [{"key": "refact.show_completion"}] },
{ "keys": ["escape"], "command": "refact_clear_completion", "context": [{"key": "refact.show_completion"}] },
{ "keys": ["ctrl+p"], "command": "refact_pause" }
]
39 changes: 38 additions & 1 deletion Main.sublime-menu
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,42 @@
"args":{}
},
]
}
},
{
"caption": "Preferences",
"mnemonic": "n",
"id": "preferences",
"children":
[
{
"caption": "Package Settings",
"mnemonic": "P",
"id": "package-settings",
"children":
[
{
"caption": "refact",
"children":
[
{
"command": "open_file", "args":
{
"file": "${packages}/refact/refact.sublime-settings"
},
"caption": "Settings – Default"
},
{
"command": "open_file", "args":
{
"file": "${packages}/User/refact.sublime-settings"
},
"caption": "Settings – User"
},
{ "caption": "-" }
]
}
]
}
]
}
]
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ Refact for VS Code is a free, open-source AI code assistant
4. Move the folder repsository to sublime's "Packages" folder (you can find this by opening the command prompt in sublime and typing "browse packages")
5. rename the folder to "refact"
6. Open refact.sublime-settings and add the API key


#Pause
You can pause and unpause refact suggestions by pressing ctrl + p

#File Documentation#

#__init__.py
Expand Down
48 changes: 38 additions & 10 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,20 @@ def on_modified(self, view):
session = refact_session_manager.get_session(view)
session.notify_document_update()
session.update_completion()

def on_close(self, view):
if not start_refact:
return

session = refact_session_manager.get_session(view)
session.notify_close()

def on_post_save(self, view):
if not start_refact:
return

session = refact_session_manager.get_session(view)
session.notify_save()

def on_query_context(self, view, key, operator, operand, match_all):
if start_refact:
Expand Down Expand Up @@ -52,6 +65,7 @@ def on_text_command(self, view, command_name, args):
elif command_name == "hide_popup":
session.clear_completion()
# elif command_name == "left_delete" or command_name == "right_delete":

# session.clear_completion()
elif command_name == "move" or command_name == "move_to":
session.clear_completion()
Expand All @@ -63,17 +77,24 @@ def plugin_loaded():
global start_refact
s = sublime.load_settings("refact.sublime-settings")
pause_completion = s.get("pause_completion", False)
if not pause_completion:
if pause_completion:
sublime.status_message("⏸️ refact.ai")
else:
refact_start()

def refact_start():
global refact_session_manager
global start_refact
if refact_session_manager:
refact_session_manager.start()
else:
refact_session_manager = RefactSessionManager()
start_refact= True
start_refact= True

class RefactStartCommand(sublime_plugin.TextCommand):
def run(self, edit):
global refact_session_manager
global start_refact
refact_session_manager = RefactSessionManager()
start_refact= True

refact_start()

class RefactStopCommand(sublime_plugin.TextCommand):
def run(self, edit):
global start_refact
Expand Down Expand Up @@ -123,11 +144,18 @@ def run(self, edit):
global start_refact
start_refact= False
s = sublime.load_settings("refact.sublime-settings")
s.set("pause_completion", True)
pause_status = s.get("pause_completion", False)
pause_status = not pause_status
s.set("pause_completion", pause_status)
sublime.save_settings("refact.sublime-settings")
refact_session_manager.get_session(self.view).clear_completion()

if not pause_status:
refact_start()
else:
if refact_session_manager:
refact_session_manager.shutdown()

class RefactClearCompletion(sublime_plugin.TextCommand):
def run(self, edit):
refact_session_manager.get_session(self.view).clear_completion()

13 changes: 12 additions & 1 deletion src/completion_text.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .utils import *

def get_nonwhitespace(s, start):
def get_nonwhitespace(s, start = 0):
end = len(s)
for i in range(start, end):
if not s[i].isspace():
Expand Down Expand Up @@ -43,6 +44,16 @@ def collect_space(s, index):
return space

def get_completion_text(point, text, line, end = None):
if not line or line.isspace():
s = replace_tab(text)
res_space = get_nonwhitespace(s)
l = replace_tab(line)
diff = res_space - len(l)
if diff > 0:
return s[(res_space - diff):]
else:
return s[res_space:]

diff = find_diff(text, line)
end = end or len(line)

Expand Down
6 changes: 2 additions & 4 deletions src/phantom_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ def __init__(self, view, phantom_block):
self.empty = (not self.cursor_phantom or len(self.cursor_phantom.text) == 0) and not self.next_inline_phantom and not self.next_line_phantom

def is_cursor_on_line(self, view, line):
if self.cursor_phantom.line <= view.rowcol(line.b)[0]:
phantom_line = self.cursor_phantom.get_line(view)
return phantom_line.intersects(line)
return False
return self.cursor_phantom.line == view.rowcol(line.b)[0]

def create_phantom(self, view, position, text):
return PhantomSeed(view, position, text)
Expand Down Expand Up @@ -218,6 +215,7 @@ def get_update_meta(self, seed):
line = view.line(cursor_point)

if not seed.is_cursor_on_line(view, line):
self.clear_phantoms()
return None

completion_text = self.get_seed_completion_text(seed)
Expand Down
80 changes: 65 additions & 15 deletions src/refact_lsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,87 @@ def __init__(self, process, statusbar):
self.connect(process)

def load_document(self, file_name: str, text: str, version: int = 1, languageId = LANGUAGE_IDENTIFIER.PYTHON):
print("load_document", file_name)

if languageId is None:
languageId = LANGUAGE_IDENTIFIER.PYTHON

if file_name is None:
return

uri = pathlib.Path(file_name).as_uri()
try:
self.lsp_client.didOpen(TextDocumentItem(uri, languageId, version, text=text))
except:
self.lsp_client.didOpen(TextDocumentItem(uri, languageId, version=version, text=text))
except Exception as err:
self.statusbar.handle_err(err)
print("lsp didOpen error")

def did_change(self, file_name: str, version: int, text: str, languageId = LANGUAGE_IDENTIFIER.PYTHON):
print("did_change file_name", file_name)

if languageId is None:
languageId = LANGUAGE_IDENTIFIER.PYTHON

if file_name is None:
return

uri = pathlib.Path(file_name).as_uri()

try:
self.lsp_client.didChange(TextDocumentItem(uri, languageId, version, text=text), [TextDocumentContentChangeEvent(None, None, text)])
except:
except Exception as err:
self.statusbar.handle_err(err)
print("lsp didChange error")

def did_save(self, file_name: str, version: int, text: str, languageId = LANGUAGE_IDENTIFIER.PYTHON):
print("did_save file_name", file_name)

if languageId is None:
languageId = LANGUAGE_IDENTIFIER.PYTHON

if file_name is None:
return

uri = pathlib.Path(file_name).as_uri()

try:
self.lsp_client.lsp_endpoint.send_notification("textDocument/didSave", textDocument=TextDocumentItem(uri, languageId, version, text=text))

except Exception as err:
self.statusbar.handle_err(err)

print("lsp didChange error", str(err))

def did_close(self, file_name: str, version: int, text: str, languageId = LANGUAGE_IDENTIFIER.PYTHON):
print("did_close file_name", file_name)

if languageId is None:
languageId = LANGUAGE_IDENTIFIER.PYTHON

if file_name is None:
return

uri = pathlib.Path(file_name).as_uri()

try:
self.lsp_client.lsp_endpoint.send_notification("textDocument/didClose", textDocument=TextDocumentItem(uri, languageId, version, text=text))
except Exception as err:
print("lsp did_close error")
self.statusbar.handle_err(err)

def get_completions(self, file_name, pos: Tuple[int, int], multiline: bool = False):
self.statusbar.update_statusbar("loading")
params = {
"max_new_tokens": 20,
"temperature": 0.1
}

if file_name is None:
return

uri = pathlib.Path(file_name).as_uri()
try:

try:
res = self.lsp_endpoint.call_method(
"refact/getCompletions",
textDocument=TextDocumentIdentifier(uri),
Expand All @@ -54,29 +103,30 @@ def get_completions(self, file_name, pos: Tuple[int, int], multiline: bool = Fal
self.statusbar.update_statusbar("ok")
return res
except Exception as err:
self.statusbar.update_statusbar("error", str(type(err)))
self.statusbar.handle_err(err)

def shutdown(self):
try:
self.lsp_client.shutdown()
except:
except Exception as err:
self.statusbar.handle_err(err)

print("lsp error shutdown")

def logMessage(self, args):
print("logMessage", args)

def connect(self, process):
capabilities = {}
# s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# s.connect(("127.0.0.1", 8002))
# pipein, pipeout = s.makefile("wb", buffering=0), s.makefile("rb", buffering=0)

# json_rpc_endpoint = JsonRpcEndpoint(pipein, pipeout)
json_rpc_endpoint = JsonRpcEndpoint(process.stdin, process.stdout)
self.lsp_endpoint = LspEndpoint(json_rpc_endpoint)
self.lsp_endpoint = LspEndpoint(json_rpc_endpoint, notify_callbacks = {"window/logMessage":print})
self.lsp_client = LspClient(self.lsp_endpoint)

try:
self.lsp_client.initialize(process.pid, None, None, None, capabilities, "off", None)
except Exception as err:
self.statusbar.update_statusbar("error", str(type(err)))
print("lsp initialize error")
self.statusbar.handle_err(err)
print("lsp initialize error", err)

def get_language_id(file_type):
if file_type and not file_type.isspace():
Expand Down
29 changes: 8 additions & 21 deletions src/refact_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@ def __init__(self):
self.active = False
self.statusbar = StatusBar()

def process_server_errors(self):
process = self.process

stderr = self.process.stderr
while process.poll() is None:
line = stderr.readline().decode('utf-8')
print(line)
if "error" in line:
self.statusbar.update_statusbar("error", line)

def get_server_path(self):
return os.path.join(sublime.packages_path(), "refact", "server", "refact-lsp")

Expand Down Expand Up @@ -51,19 +41,16 @@ def get_server_commands(self):
def start_server(self):
self.active = True
server_cmds = self.get_server_commands()
self.process = subprocess.Popen(server_cmds, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr=subprocess.PIPE)
self.poll_server()
t = threading.Thread(target=self.process_server_errors)
t.start()

self.process = subprocess.Popen(server_cmds, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr=subprocess.PIPE, shell=False)

self.statusbar.update_statusbar("ok")
if not self.connection is None:
self.connection.shutdown()

self.connection = LSP(self.process, self.statusbar)

def poll_server(self):
didCrash = self.process.poll()
if not didCrash is None:
self.active = False
else:
sublime.set_timeout(self.poll_server, 100)
def stop_server(self):
self.connection.shutdown()
self.process.terminate()
self.statusbar.update_statusbar("pause")

Loading

0 comments on commit 8071dc0

Please sign in to comment.