Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setting changes now cause server to restart #16

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
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
42 changes: 36 additions & 6 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,24 @@

start_refact = False
refact_session_manager = None
open_folders = {}

class RefactAutocomplete(sublime_plugin.EventListener):
def on_query_completions(self, view, prefix, locations):
if start_refact:
refact_session_manager.get_session(view).show_completions(prefix, locations)

def on_new_window(self, window):
sublime.set_timeout(lambda: restart_on_new_folder(window), 100)

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

session = refact_session_manager.get_session(view)
session.notify_document_update()
session.update_completion()

def on_close(self, view):
if not start_refact:
return
Expand All @@ -40,7 +44,7 @@ def on_query_context(self, view, key, operator, operand, match_all):
if start_refact:
if key == "refact.show_completion":
return refact_session_manager.get_session(view).completion_visible()

def on_post_text_command(self, view, command_name, args):
if start_refact:
session = refact_session_manager.get_session(view)
Expand Down Expand Up @@ -72,23 +76,50 @@ def on_text_command(self, view, command_name, args):
elif command_name == "drag_select":
session.clear_completion()

def restart_server():
global refact_session_manager

if refact_session_manager:
s = sublime.load_settings("refact.sublime-settings")
pause_completion = s.get("pause_completion", False)

if not pause_completion:
refact_session_manager.restart_server()

def plugin_loaded():
global refact_session_manager
global start_refact
global open_folders
windows = sublime.windows()
open_folders = {folder for w in windows for folder in w.folders()}
s = sublime.load_settings("refact.sublime-settings")
pause_completion = s.get("pause_completion", False)
s.add_on_change("restart_server", restart_server)
if pause_completion:
sublime.status_message("⏸️ refact.ai")
else:
refact_start()

def restart_on_new_folder(window):
global open_folders

window_folders = set(window.folders())
new_folders = window_folders - open_folders
if len(new_folders) > 0:
open_folders |= new_folders
restart_server()

def get_start_refact():
print("get_start_refact", start_refact)
return start_refact

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

class RefactStartCommand(sublime_plugin.TextCommand):
Expand Down Expand Up @@ -142,14 +173,14 @@ def run(self, edit):
class RefactPause(sublime_plugin.TextCommand):
def run(self, edit):
global start_refact
start_refact= False
s = sublime.load_settings("refact.sublime-settings")
pause_status = s.get("pause_completion", False)
pause_status = not pause_status
start_refact = not pause_status
s.set("pause_completion", pause_status)
sublime.save_settings("refact.sublime-settings")

if not pause_status:
if not pause_status and refact_session_manager is None:
refact_start()
else:
if refact_session_manager:
Expand All @@ -158,4 +189,3 @@ def run(self, edit):
class RefactClearCompletion(sublime_plugin.TextCommand):
def run(self, edit):
refact_session_manager.get_session(self.view).clear_completion()

2 changes: 1 addition & 1 deletion refact.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"code_completion_model": "",
"code_completion_scratchpad": "",
"pause_completion": false,
"telemetry_code_snippets": false
"telemetry_basic": true
// Use something like
// tail -f -n 1000 ~/.cache/refact/logs/rustbinary.2024-02-07
// to see what's going on with the server
Expand Down
11 changes: 6 additions & 5 deletions src/completion_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ def collect_space(s, index):

def get_completion_text(point, text, line, end = None):
if not line or line.isspace():
s = replace_tab(text)
tab_size = get_tab_size()

s = replace_tab(text, tab_size)
res_space = get_nonwhitespace(s)
l = replace_tab(line)
diff = res_space - len(l)
if diff > 0:
return s[(res_space - diff):]
line_len = len(replace_tab(line, tab_size))
if res_space > line_len:
return s[line_len:]
else:
return s[res_space:]

Expand Down
15 changes: 11 additions & 4 deletions src/phantom_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sublime_plugin
import html
from .utils import *
from .completion_text import get_completion_text
from .completion_text import get_completion_text, get_nonwhitespace
from dataclasses import dataclass
from typing import NamedTuple

Expand Down Expand Up @@ -107,7 +107,7 @@ def __init__(self, view):
self.update_step = False
self.phantoms_visible = False
s = sublime.load_settings("Preferences.sublime-settings")
self.tab_size = s.get("tab_size", 4)
self.tab_size = get_tab_size()

invisibleDiv = """
<body id="invisible-div">
Expand Down Expand Up @@ -176,10 +176,17 @@ def show_start_line_completion(self, position, text):
return

previous_line = get_previous_line(self.view, position)
previous_line_text = self.view.substr(previous_line)

popup_point = previous_line.a
if len(previous_line) > 0:
if previous_line_text[0] == '\t':
popup_point = previous_line.a
text = " " + text
elif len(previous_line) > 0:
popup_point = popup_point + 1
self.view.show_popup(self.create_annotation_template(text), location = popup_point, flags = sublime.PopupFlags.HIDE_ON_CHARACTER_EVENT, max_width = 999)

popup_text = self.create_annotation_template(text)
self.view.show_popup(popup_text, location = popup_point, flags = sublime.PopupFlags.HIDE_ON_CHARACTER_EVENT, max_width = 999)

def add_phantoms(self, new_phantoms):
view = self.view
Expand Down
17 changes: 10 additions & 7 deletions src/refact_lsp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import sublime
import os
import socket
import pathlib
import traceback
from typing import Optional, Dict, Tuple
from .pylspclient.lsp_structs import *
from .pylspclient.lsp_endpoint import LspEndpoint
Expand All @@ -12,7 +14,7 @@ def __init__(self, process, statusbar):
self.statusbar = statusbar
self.connect(process)

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

if languageId is None:
Expand Down Expand Up @@ -104,26 +106,27 @@ def get_completions(self, file_name, pos: Tuple[int, int], multiline: bool = Fal
return res
except Exception as err:
self.statusbar.handle_err(err)
return


def shutdown(self):
try:
self.lsp_client.shutdown()
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 = {}
json_rpc_endpoint = JsonRpcEndpoint(process.stdin, process.stdout)
self.lsp_endpoint = LspEndpoint(json_rpc_endpoint, notify_callbacks = {"window/logMessage":print})
self.lsp_client = LspClient(self.lsp_endpoint)

windows = sublime.windows()
workspaces = [{'name': folder, 'uri': pathlib.Path(folder).as_uri()} for w in windows for folder in w.folders()]

print("workspaces: ", workspaces)
try:
self.lsp_client.initialize(process.pid, None, None, None, capabilities, "off", None)
self.lsp_client.initialize(process.pid, None, None, None, capabilities, "off", workspaces)
except Exception as err:
self.statusbar.handle_err(err)
print("lsp initialize error", err)
Expand Down
20 changes: 14 additions & 6 deletions src/refact_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ class RefactProcessWrapper():
def __init__(self):
self.connection = None
self.active = False
self.process = None
self.statusbar = StatusBar()

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

def get_server_commands(self):
s = sublime.load_settings("refact.sublime-settings")

Expand All @@ -41,11 +42,16 @@ def get_server_commands(self):
def start_server(self):
self.active = True
server_cmds = self.get_server_commands()
startupinfo = None
if os.name == 'nt':

if not self.process is None:
self.process.kill()

if hasattr(subprocess, 'STARTUPINFO'):
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
self.process = subprocess.Popen(server_cmds, startupinfo=startupinfo, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
self.process = subprocess.Popen(server_cmds, startupinfo=startupinfo, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
else:
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:
Expand All @@ -54,7 +60,9 @@ def start_server(self):
self.connection = LSP(self.process, self.statusbar)

def stop_server(self):
self.connection.shutdown()
if not self.connection is None:
self.connection.shutdown()
self.process.terminate()
self.active = False
self.statusbar.update_statusbar("pause")

Loading
Loading