Skip to content

Commit

Permalink
refactor: make URL schema configurable
Browse files Browse the repository at this point in the history
I plan to make TypeProf work with Monaco Editor, which uses "inmemory"
schema instead of "file" schema.
  • Loading branch information
mame committed Dec 20, 2024
1 parent 95af1f0 commit 206e82a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 23 deletions.
12 changes: 6 additions & 6 deletions lib/typeprof/lsp/messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class Message::Initialize < Message
def run
folders = @params[:workspaceFolders].map do |folder|
folder => { uri:, }
TypeProf::LSP.file_uri_to_path(uri)
@server.uri_to_path(uri)
end

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

path = TypeProf::LSP.file_uri_to_path(uri)
path = @server.uri_to_path(uri)
return unless @server.target_path?(path)

text = Text.new(path, text, version)
Expand Down Expand Up @@ -204,7 +204,7 @@ def run
else
respond(defs.map do |path, code_range|
{
uri: "file://" + path,
uri: @server.path_to_uri(path),
range: code_range.to_lsp,
}
end)
Expand All @@ -230,7 +230,7 @@ def run
else
respond(defs.map do |path, code_range|
{
uri: "file://" + path,
uri: @server.path_to_uri(path),
range: code_range.to_lsp,
}
end)
Expand All @@ -254,7 +254,7 @@ def run
if callsites
respond(callsites.map do |path, code_range|
{
uri: "file://" + path,
uri: @server.path_to_uri(path),
range: code_range.to_lsp,
}
end)
Expand Down Expand Up @@ -373,7 +373,7 @@ def run
if renames
changes = {}
renames.each do |path, cr|
(changes["file://" + path] ||= []) << {
(changes[@server.path_to_uri(path)] ||= []) << {
range: cr.to_lsp,
newText: newName,
}
Expand Down
11 changes: 10 additions & 1 deletion lib/typeprof/lsp/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def self.start_socket(core)
end
end

def initialize(core, reader, writer)
def initialize(core, reader, writer, url_schema: nil)
@core = core
@workspaces = {}
@reader = reader
Expand All @@ -55,11 +55,20 @@ def initialize(core, reader, writer)
@open_texts = {}
@exit = false
@signature_enabled = true
@url_schema = url_schema || (File::ALT_SEPARATOR != "\\" ? "file://" : "file:///")
end

attr_reader :core, :open_texts
attr_accessor :signature_enabled

def path_to_uri(path)
@url_schema + File.expand_path(path)
end

def uri_to_path(url)
url.delete_prefix(@url_schema)
end

def add_workspaces(folders)
folders.each do |path|
conf_path = File.join(path, "typeprof.conf.json")
Expand Down
10 changes: 0 additions & 10 deletions lib/typeprof/lsp/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,4 @@ def self.load_json_with_comments(path, **opts)

JSON.parse(json, **opts)
end

FILE_URL_PREFIX = File::ALT_SEPARATOR != "\\" ? "file://" : "file:///"

def self.file_path_to_uri(path)
FILE_URL_PREFIX + File.expand_path(path)
end

def self.file_uri_to_path(url)
url.delete_prefix(FILE_URL_PREFIX)
end
end
10 changes: 4 additions & 6 deletions test/lsp/lsp_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,14 @@ def write(**json)

def setup
@dummy_io = DummyIO.new
@th = Thread.new do
core = TypeProf::Core::Service.new({})
serv = TypeProf::LSP::Server.new(core, @dummy_io, @dummy_io)
serv.run
end
@core = TypeProf::Core::Service.new({})
@lsp = TypeProf::LSP::Server.new(@core, @dummy_io, @dummy_io)
@th = Thread.new { @lsp.run }
@id = 0
end

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

0 comments on commit 206e82a

Please sign in to comment.