Class: TypeProf::LSP::Server
- Inherits:
-
Object
- Object
- TypeProf::LSP::Server
- Defined in:
- lib/typeprof/lsp/server.rb
Instance Attribute Summary collapse
-
#open_texts ⇒ Object
readonly
Returns the value of attribute open_texts.
-
#signature_enabled ⇒ Object
Returns the value of attribute signature_enabled.
Class Method Summary collapse
Instance Method Summary collapse
-
#add_workspaces(folders) ⇒ Object
: (Array) -> void.
- #aggregate_each_core(path) ⇒ Object
- #cancel_request(id) ⇒ Object
- #code_lens(path, &blk) ⇒ Object
- #completion(path, trigger, pos, &blk) ⇒ Object
- #definitions(path, pos) ⇒ Object
- #each_core(path) ⇒ Object
- #exit ⇒ Object
- #hover(path, pos) ⇒ Object
-
#initialize(core_options, reader, writer, url_schema: nil) ⇒ Server
constructor
A new instance of Server.
-
#path_to_uri(path) ⇒ Object
: (String) -> String.
- #publish_updated_diagnostics ⇒ Object
- #references(path, pos) ⇒ Object
- #rename(path, pos) ⇒ Object
- #run ⇒ Object
- #send_notification(method, **params) ⇒ Object
- #send_request(method, **params, &blk) ⇒ Object
- #send_response(**msg) ⇒ Object
-
#target_path?(path) ⇒ Boolean
: (String) -> bool.
- #type_definitions(path, pos) ⇒ Object
- #update_file(path, text) ⇒ Object
- #uri_to_path(uri) ⇒ Object
Constructor Details
#initialize(core_options, reader, writer, url_schema: nil) ⇒ Server
Returns a new instance of Server.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/typeprof/lsp/server.rb', line 50 def initialize(, reader, writer, url_schema: nil) @core_options = @cores = {} @reader = reader @writer = writer @request_id = 0 @running_requests_from_client = {} @running_requests_from_server = {} @open_texts = {} @exit = false @signature_enabled = true @url_schema = url_schema || (File::ALT_SEPARATOR != "\\" ? "file://" : "file:///") @diagnostic_severity = :error end |
Instance Attribute Details
#open_texts ⇒ Object (readonly)
Returns the value of attribute open_texts.
65 66 67 |
# File 'lib/typeprof/lsp/server.rb', line 65 def open_texts @open_texts end |
#signature_enabled ⇒ Object
Returns the value of attribute signature_enabled.
66 67 68 |
# File 'lib/typeprof/lsp/server.rb', line 66 def signature_enabled @signature_enabled end |
Class Method Details
.start_socket(core_options) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/typeprof/lsp/server.rb', line 24 def self.start_socket() Socket.tcp_server_sockets("localhost", nil) do |servs| serv = servs[0].local_address $stdout << JSON.generate({ host: serv.ip_address, port: serv.ip_port, pid: $$, }) $stdout.flush $stdout = $stderr Socket.accept_loop(servs) do |sock| sock.set_encoding("UTF-8") begin reader = Reader.new(sock) writer = Writer.new(sock) new(, reader, writer).run ensure sock.close end exit end end end |
.start_stdio(core_options) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/typeprof/lsp/server.rb', line 14 def self.start_stdio() $stdin.binmode $stdout.binmode reader = Reader.new($stdin) writer = Writer.new($stdout) # pipe all builtin print output to stderr to avoid conflicting with lsp $stdout = $stderr new(, reader, writer).run end |
Instance Method Details
#add_workspaces(folders) ⇒ Object
: (Array) -> void
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/typeprof/lsp/server.rb', line 78 def add_workspaces(folders) folders.each do |path| conf_path = [".json", ".jsonc"].map do |ext| File.join(path, "typeprof.conf" + ext) end.find do |path| File.readable?(path) end unless conf_path puts "typeprof.conf.json is not found in #{ path }" next end conf = TypeProf::LSP.load_json_with_comments(conf_path, symbolize_names: true) if conf if conf[:rbs_dir] rbs_dir = File.(conf[:rbs_dir]) else rbs_dir = File.(File.("sig", path)) end @rbs_dir = rbs_dir if conf[:typeprof_version] == "experimental" if conf[:diagnostic_severity] severity = conf[:diagnostic_severity].to_sym case severity when :error, :warning, :info, :hint @diagnostic_severity = severity else puts "unknown severity: #{ severity }" end end conf[:analysis_unit_dirs].each do |dir| dir = File.(dir, path) core = @cores[dir] = TypeProf::Core::Service.new(@core_options) core.add_workspace(dir, @rbs_dir) end else puts "Unknown typeprof_version: #{ conf[:typeprof_version] }" end end end end |
#aggregate_each_core(path) ⇒ Object
136 137 138 139 140 141 142 143 |
# File 'lib/typeprof/lsp/server.rb', line 136 def aggregate_each_core(path) ret = [] each_core(path) do |core| r = yield(core) ret.concat(r) if r end ret end |
#cancel_request(id) ⇒ Object
231 232 233 234 |
# File 'lib/typeprof/lsp/server.rb', line 231 def cancel_request(id) req = @running_requests_from_client[id] req.cancel if req.respond_to?(:cancel) end |
#code_lens(path, &blk) ⇒ Object
177 178 179 180 181 |
# File 'lib/typeprof/lsp/server.rb', line 177 def code_lens(path, &blk) each_core(path) do |core| core.code_lens(path, &blk) end end |
#completion(path, trigger, pos, &blk) ⇒ Object
183 184 185 186 187 |
# File 'lib/typeprof/lsp/server.rb', line 183 def completion(path, trigger, pos, &blk) each_core(path) do |core| core.completion(path, trigger, pos, &blk) end end |
#definitions(path, pos) ⇒ Object
151 152 153 154 155 |
# File 'lib/typeprof/lsp/server.rb', line 151 def definitions(path, pos) aggregate_each_core(path) do |core| core.definitions(path, pos) end end |
#each_core(path) ⇒ Object
128 129 130 131 132 133 134 |
# File 'lib/typeprof/lsp/server.rb', line 128 def each_core(path) @cores.each do |folder, core| if path.start_with?(folder) || @rbs_dir && path.start_with?(@rbs_dir) yield core end end end |
#exit ⇒ Object
236 237 238 |
# File 'lib/typeprof/lsp/server.rb', line 236 def exit @exit = true end |
#hover(path, pos) ⇒ Object
169 170 171 172 173 174 175 |
# File 'lib/typeprof/lsp/server.rb', line 169 def hover(path, pos) ret = [] each_core(path) do |core| ret << core.hover(path, pos) end ret.compact.first # TODO end |
#path_to_uri(path) ⇒ Object
: (String) -> String
69 70 71 |
# File 'lib/typeprof/lsp/server.rb', line 69 def path_to_uri(path) @url_schema + File.(path).split("/").map {|s| CGI.escapeURIComponent(s) }.join("/") end |
#publish_updated_diagnostics ⇒ Object
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/typeprof/lsp/server.rb', line 240 def publish_updated_diagnostics @cores.each do |_, core| diags = [] core.process_diagnostic_paths do |path| uri = path_to_uri(path) next false unless @open_texts[uri] core.diagnostics(path) do |diag| diags << diag.to_lsp(severity: @diagnostic_severity) end send_notification( "textDocument/publishDiagnostics", uri: uri, diagnostics: diags ) true end end end |
#references(path, pos) ⇒ Object
163 164 165 166 167 |
# File 'lib/typeprof/lsp/server.rb', line 163 def references(path, pos) aggregate_each_core(path) do |core| core.references(path, pos) end end |
#rename(path, pos) ⇒ Object
189 190 191 192 193 |
# File 'lib/typeprof/lsp/server.rb', line 189 def rename(path, pos) aggregate_each_core(path) do |core| core.rename(path, pos) end end |
#run ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/typeprof/lsp/server.rb', line 195 def run @reader.read do |json| if json[:method] # request or notification msg_class = Message.find(json[:method]) if msg_class msg = msg_class.new(self, json) @running_requests_from_client[json[:id]] = msg if json[:id] msg.run else end else # response callback = @running_requests_from_server.delete(json[:id]) callback&.call(json[:params], json[:error]) end break if @exit end end |
#send_notification(method, **params) ⇒ Object
221 222 223 |
# File 'lib/typeprof/lsp/server.rb', line 221 def send_notification(method, **params) @writer.write(method: method, params: params) end |
#send_request(method, **params, &blk) ⇒ Object
225 226 227 228 229 |
# File 'lib/typeprof/lsp/server.rb', line 225 def send_request(method, **params, &blk) id = @request_id += 1 @running_requests_from_server[id] = blk @writer.write(id: id, method: method, params: params) end |
#send_response(**msg) ⇒ Object
216 217 218 219 |
# File 'lib/typeprof/lsp/server.rb', line 216 def send_response(**msg) @running_requests_from_client.delete(msg[:id]) @writer.write(**msg) end |
#target_path?(path) ⇒ Boolean
: (String) -> bool
120 121 122 123 124 125 126 |
# File 'lib/typeprof/lsp/server.rb', line 120 def target_path?(path) return true if @rbs_dir && path.start_with?(@rbs_dir) @cores.each do |folder, _| return true if path.start_with?(folder) end return false end |
#type_definitions(path, pos) ⇒ Object
157 158 159 160 161 |
# File 'lib/typeprof/lsp/server.rb', line 157 def type_definitions(path, pos) aggregate_each_core(path) do |core| core.type_definitions(path, pos) end end |
#update_file(path, text) ⇒ Object
145 146 147 148 149 |
# File 'lib/typeprof/lsp/server.rb', line 145 def update_file(path, text) each_core(path) do |core| core.update_file(path, text) end end |
#uri_to_path(uri) ⇒ Object
73 74 75 |
# File 'lib/typeprof/lsp/server.rb', line 73 def uri_to_path(uri) uri.delete_prefix(@url_schema).split("/").map {|s| CGI.unescapeURIComponent(s) }.join("/") end |