Skip to content

Commit c04ca6b

Browse files
committed
Auto-detect UNIX sockets, support relative paths
1 parent 0431d6d commit c04ca6b

File tree

4 files changed

+39
-14
lines changed

4 files changed

+39
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
### WIP
22

33
- Eval previous form at current level #118
4+
- Auto-detect UNIX sockets, support relative paths
45

56
### 4.0.0 - Aug 23, 2024
67

cs_common.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,23 @@ def phantom_styles(view, scope):
195195
except:
196196
pass
197197

198+
def find_in_folders(name = None, pred = None):
199+
if window := sublime.active_window():
200+
if name and os.path.isabs(name):
201+
if os.path.exists(name):
202+
return name
203+
else:
204+
return None
205+
for folder in window.folders():
206+
if name:
207+
path = folder + "/" + name
208+
if os.path.exists(path):
209+
return path
210+
if pred:
211+
for file in os.listdir(folder):
212+
path = folder + "/" + file
213+
if pred(folder + '/' + file):
214+
return path
198215

199216
class SocketIO:
200217
"""
@@ -221,7 +238,7 @@ def socket_connect(addr):
221238
return socket.create_connection((host, port))
222239
else: # path == unix domain socket
223240
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
224-
s.connect(addr)
241+
s.connect(find_in_folders(addr))
225242
return s
226243

227244
def set_status(window, key, value):

cs_conn.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import os, re, sublime, sublime_plugin, threading, time
1+
import os, re, stat, sublime, sublime_plugin, threading, time
22
from . import cs_common, cs_eval, cs_eval_status, cs_parser, cs_warn
33

44
status_key = 'clojure-sublimed-conn'
@@ -174,6 +174,9 @@ def set_status(self, phase, message, *args):
174174
self.status = status
175175
cs_common.set_status(self.window, status_key, status)
176176

177+
def is_socket(path):
178+
return stat.S_ISSOCK(os.stat(path).st_mode)
179+
177180
class AddressInputHandler(sublime_plugin.TextInputHandler):
178181
def __init__(self, port_files = [], next_input = None):
179182
self.port_files = port_files
@@ -187,14 +190,15 @@ def placeholder(self):
187190

188191
def initial_text(self):
189192
# .nrepl-port file present
190-
if self.port_files and (window := sublime.active_window()):
191-
for folder in window.folders():
192-
for port_file in self.port_files:
193-
if os.path.exists(folder + "/" + port_file):
194-
with open(folder + "/" + port_file, "rt") as f:
195-
content = f.read(10).strip()
196-
if re.fullmatch(r'[1-9][0-9]*', content):
197-
return f'localhost:{content}'
193+
if self.port_files:
194+
for port_file in self.port_files:
195+
if path := cs_common.find_in_folders(name = port_file):
196+
with open(path, "rt") as f:
197+
content = f.read(10).strip()
198+
if re.fullmatch(r'[1-9][0-9]*', content):
199+
return f'localhost:{content}'
200+
if path := cs_common.find_in_folders(pred = is_socket):
201+
return path
198202
state = cs_common.get_state()
199203
return state.last_conn[1]['address'] if state.last_conn else 'localhost:'
200204

@@ -212,14 +216,17 @@ def preview(self, text):
212216

213217
def validate(self, text):
214218
text = text.strip()
215-
if 'auto' == text:
219+
if not text:
220+
return False
221+
elif 'auto' == text:
216222
return True
217223
elif match := re.fullmatch(r'([a-zA-Z0-9\.]+):(\d{1,5})', text):
218224
_, port = match.groups()
219225
return 1 <= int(port) and int(port) < 65536
220226
else:
221-
return os.path.isfile(text)
222-
227+
path = cs_common.find_in_folders(name = text)
228+
return bool(path and is_socket(path))
229+
223230
def next_input(self, args):
224231
return self.next
225232

script/nrepl_socket.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
if __name__ == '__main__':
55
os.chdir(os.path.dirname(__file__) + "/..")
66
subprocess.check_call(['clojure',
7-
'-Sdeps', '{:deps {nrepl/nrepl {:mvn/version "1.0.0"}}}',
7+
'-Sdeps', '{:deps {nrepl/nrepl {:mvn/version "1.3.0"}}}',
88
'-M', '-m', 'nrepl.cmdline',
99
'--socket', 'nrepl-socket'
1010
])

0 commit comments

Comments
 (0)