Skip to content

Commit 206e82a

Browse files
committed
refactor: make URL schema configurable
I plan to make TypeProf work with Monaco Editor, which uses "inmemory" schema instead of "file" schema.
1 parent 95af1f0 commit 206e82a

4 files changed

Lines changed: 20 additions & 23 deletions

File tree

lib/typeprof/lsp/messages.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class Message::Initialize < Message
7272
def run
7373
folders = @params[:workspaceFolders].map do |folder|
7474
folder => { uri:, }
75-
TypeProf::LSP.file_uri_to_path(uri)
75+
@server.uri_to_path(uri)
7676
end
7777

7878
@server.add_workspaces(folders)
@@ -146,7 +146,7 @@ class Message::TextDocument::DidOpen < Message
146146
def run
147147
@params => { textDocument: { uri:, version:, text: } }
148148

149-
path = TypeProf::LSP.file_uri_to_path(uri)
149+
path = @server.uri_to_path(uri)
150150
return unless @server.target_path?(path)
151151

152152
text = Text.new(path, text, version)
@@ -204,7 +204,7 @@ def run
204204
else
205205
respond(defs.map do |path, code_range|
206206
{
207-
uri: "file://" + path,
207+
uri: @server.path_to_uri(path),
208208
range: code_range.to_lsp,
209209
}
210210
end)
@@ -230,7 +230,7 @@ def run
230230
else
231231
respond(defs.map do |path, code_range|
232232
{
233-
uri: "file://" + path,
233+
uri: @server.path_to_uri(path),
234234
range: code_range.to_lsp,
235235
}
236236
end)
@@ -254,7 +254,7 @@ def run
254254
if callsites
255255
respond(callsites.map do |path, code_range|
256256
{
257-
uri: "file://" + path,
257+
uri: @server.path_to_uri(path),
258258
range: code_range.to_lsp,
259259
}
260260
end)
@@ -373,7 +373,7 @@ def run
373373
if renames
374374
changes = {}
375375
renames.each do |path, cr|
376-
(changes["file://" + path] ||= []) << {
376+
(changes[@server.path_to_uri(path)] ||= []) << {
377377
range: cr.to_lsp,
378378
newText: newName,
379379
}

lib/typeprof/lsp/server.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def self.start_socket(core)
4444
end
4545
end
4646

47-
def initialize(core, reader, writer)
47+
def initialize(core, reader, writer, url_schema: nil)
4848
@core = core
4949
@workspaces = {}
5050
@reader = reader
@@ -55,11 +55,20 @@ def initialize(core, reader, writer)
5555
@open_texts = {}
5656
@exit = false
5757
@signature_enabled = true
58+
@url_schema = url_schema || (File::ALT_SEPARATOR != "\\" ? "file://" : "file:///")
5859
end
5960

6061
attr_reader :core, :open_texts
6162
attr_accessor :signature_enabled
6263

64+
def path_to_uri(path)
65+
@url_schema + File.expand_path(path)
66+
end
67+
68+
def uri_to_path(url)
69+
url.delete_prefix(@url_schema)
70+
end
71+
6372
def add_workspaces(folders)
6473
folders.each do |path|
6574
conf_path = File.join(path, "typeprof.conf.json")

lib/typeprof/lsp/util.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,4 @@ def self.load_json_with_comments(path, **opts)
4848

4949
JSON.parse(json, **opts)
5050
end
51-
52-
FILE_URL_PREFIX = File::ALT_SEPARATOR != "\\" ? "file://" : "file:///"
53-
54-
def self.file_path_to_uri(path)
55-
FILE_URL_PREFIX + File.expand_path(path)
56-
end
57-
58-
def self.file_uri_to_path(url)
59-
url.delete_prefix(FILE_URL_PREFIX)
60-
end
6151
end

test/lsp/lsp_test.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,14 @@ def write(**json)
3030

3131
def setup
3232
@dummy_io = DummyIO.new
33-
@th = Thread.new do
34-
core = TypeProf::Core::Service.new({})
35-
serv = TypeProf::LSP::Server.new(core, @dummy_io, @dummy_io)
36-
serv.run
37-
end
33+
@core = TypeProf::Core::Service.new({})
34+
@lsp = TypeProf::LSP::Server.new(@core, @dummy_io, @dummy_io)
35+
@th = Thread.new { @lsp.run }
3836
@id = 0
3937
end
4038

4139
def init(fixture)
42-
@folder = TypeProf::LSP.file_path_to_uri(File.expand_path(File.join(__dir__, "..", "fixtures", fixture))) + "/"
40+
@folder = @lsp.path_to_uri(File.expand_path(File.join(__dir__, "..", "fixtures", fixture))) + "/"
4341
id = request("initialize", workspaceFolders: [{ uri: @folder }])
4442
expect_response(id) do |recv|
4543
assert_equal({ name: "typeprof", version: TypeProf::VERSION }, recv[:serverInfo])

0 commit comments

Comments
 (0)