Class: Steep::Server::InteractionWorker
- Inherits:
-
BaseWorker
- Object
- BaseWorker
- Steep::Server::InteractionWorker
- Defined in:
- lib/steep/server/interaction_worker.rb
Constant Summary
Constants inherited from BaseWorker
Constants included from Utils
Instance Attribute Summary collapse
-
#queue ⇒ Object
readonly
Returns the value of attribute queue.
Attributes inherited from BaseWorker
Instance Method Summary collapse
- #format_completion_item(item) ⇒ Object
- #format_hover(content) ⇒ Object
- #handle_job(job) ⇒ Object
- #handle_request(request) ⇒ Object
-
#initialize(project:, reader:, writer:, queue: Queue.new) ⇒ InteractionWorker
constructor
A new instance of InteractionWorker.
- #method_type_to_snippet(method_type) ⇒ Object
- #params_to_snippet(fun) ⇒ Object
- #response_to_completion(path:, line:, column:, trigger:) ⇒ Object
- #response_to_hover(path:, line:, column:) ⇒ Object
Methods inherited from BaseWorker
Methods included from Utils
Constructor Details
#initialize(project:, reader:, writer:, queue: Queue.new) ⇒ InteractionWorker
6 7 8 9 |
# File 'lib/steep/server/interaction_worker.rb', line 6 def initialize(project:, reader:, writer:, queue: Queue.new) super(project: project, reader: reader, writer: writer) @queue = queue end |
Instance Attribute Details
#queue ⇒ Object (readonly)
Returns the value of attribute queue.
4 5 6 |
# File 'lib/steep/server/interaction_worker.rb', line 4 def queue @queue end |
Instance Method Details
#format_completion_item(item) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/steep/server/interaction_worker.rb', line 160 def format_completion_item(item) range = LanguageServer::Protocol::Interface::Range.new( start: LanguageServer::Protocol::Interface::Position.new( line: item.range.start.line-1, character: item.range.start.column ), end: LanguageServer::Protocol::Interface::Position.new( line: item.range.end.line-1, character: item.range.end.column ) ) case item when Project::CompletionProvider::LocalVariableItem LanguageServer::Protocol::Interface::CompletionItem.new( label: item.identifier, kind: LanguageServer::Protocol::Constant::CompletionItemKind::VARIABLE, detail: "#{item.identifier}: #{item.type}", text_edit: LanguageServer::Protocol::Interface::TextEdit.new( range: range, new_text: "#{item.identifier}" ) ) when Project::CompletionProvider::MethodNameItem label = "def #{item.identifier}: #{item.method_type}" method_type_snippet = method_type_to_snippet(item.method_type) LanguageServer::Protocol::Interface::CompletionItem.new( label: label, kind: LanguageServer::Protocol::Constant::CompletionItemKind::METHOD, text_edit: LanguageServer::Protocol::Interface::TextEdit.new( new_text: "#{item.identifier}#{method_type_snippet}", range: range ), documentation: item.definition.comment&.string, insert_text_format: LanguageServer::Protocol::Constant::InsertTextFormat::SNIPPET ) when Project::CompletionProvider::InstanceVariableItem label = "#{item.identifier}: #{item.type}" LanguageServer::Protocol::Interface::CompletionItem.new( label: label, kind: LanguageServer::Protocol::Constant::CompletionItemKind::FIELD, text_edit: LanguageServer::Protocol::Interface::TextEdit.new( range: range, new_text: item.identifier, ), insert_text_format: LanguageServer::Protocol::Constant::InsertTextFormat::SNIPPET ) end end |
#format_hover(content) ⇒ Object
77 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 118 119 120 121 122 123 124 125 |
# File 'lib/steep/server/interaction_worker.rb', line 77 def format_hover(content) case content when Project::HoverContent::VariableContent "`#{content.name}`: `#{content.type.to_s}`" when Project::HoverContent::MethodCallContent method_name = case content.method_name when Project::HoverContent::InstanceMethodName "#{content.method_name.class_name}##{content.method_name.method_name}" when Project::HoverContent::SingletonMethodName "#{content.method_name.class_name}.#{content.method_name.method_name}" else nil end if method_name string = "```\n\#{method_name} ~> \#{content.type}\n```\n" if content.definition if content.definition.comment string << "\n----\n\n#{content.definition.comment.string}" end string << "\n----\n\n#{content.definition.method_types.map {|x| "- `#{x}`\n" }.join()}" end else "`#{content.type}`" end when Project::HoverContent::DefinitionContent string = "```\ndef \#{content.method_name}: \#{content.method_type}\n```\n" if (comment = content.definition.comment) string << "\n----\n\n#{comment.string}\n" end if content.definition.method_types.size > 1 string << "\n----\n\n#{content.definition.method_types.map {|x| "- `#{x}`\n" }.join()}" end string when Project::HoverContent::TypeContent "`#{content.type}`" end end |
#handle_job(job) ⇒ Object
11 12 13 14 |
# File 'lib/steep/server/interaction_worker.rb', line 11 def handle_job(job) Steep.logger.debug "Handling job: id=#{job[:id]}, result=#{job[:result]&.to_hash}" writer.write(job) end |
#handle_request(request) ⇒ Object
16 17 18 19 20 21 22 23 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 49 50 51 |
# File 'lib/steep/server/interaction_worker.rb', line 16 def handle_request(request) case request[:method] when "initialize" # nop when "textDocument/didChange" update_source(request) when "textDocument/hover" id = request[:id] uri = URI.parse(request[:params][:textDocument][:uri]) path = project.relative_path(Pathname(uri.path)) line = request[:params][:position][:line] column = request[:params][:position][:character] queue << { id: id, result: response_to_hover(path: path, line: line, column: column) } when "textDocument/completion" id = request[:id] params = request[:params] uri = URI.parse(params[:textDocument][:uri]) path = project.relative_path(Pathname(uri.path)) line, column = params[:position].yield_self {|hash| [hash[:line]+1, hash[:character]] } trigger = params[:context][:triggerCharacter] queue << { id: id, result: response_to_completion(path: path, line: line, column: column, trigger: trigger) } end end |
#method_type_to_snippet(method_type) ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/steep/server/interaction_worker.rb', line 210 def method_type_to_snippet(method_type) params = if method_type.type.each_param.count == 0 "" else "(#{params_to_snippet(method_type.type)})" end block = if method_type.block open, space, close = if method_type.block.type.return_type.is_a?(RBS::Types::Bases::Void) ["do", " ", "end"] else ["{", "", "}"] end if method_type.block.type.each_param.count == 0 " #{open} $0 #{close}" else " #{open}#{space}|#{params_to_snippet(method_type.block.type)}| $0 #{close}" end else "" end "#{params}#{block}" end |
#params_to_snippet(fun) ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/steep/server/interaction_worker.rb', line 237 def params_to_snippet(fun) params = [] index = 1 fun.required_positionals.each do |param| if name = param.name params << "${#{index}:#{param.type}}" else params << "${#{index}:#{param.type}}" end index += 1 end if fun.rest_positionals params << "${#{index}:*#{fun.rest_positionals.type}}" index += 1 end fun.trailing_positionals.each do |param| if name = param.name params << "${#{index}:#{param.type}}" else params << "${#{index}:#{param.type}}" end index += 1 end fun.required_keywords.each do |keyword, param| if name = param.name params << "#{keyword}: ${#{index}:#{name}_}" else params << "#{keyword}: ${#{index}:#{param.type}_}" end index += 1 end params.join(", ") end |
#response_to_completion(path:, line:, column:, trigger:) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/steep/server/interaction_worker.rb', line 127 def response_to_completion(path:, line:, column:, trigger:) Steep.logger.tagged("#response_to_completion") do Steep.logger.info "path: #{path}, line: #{line}, column: #{column}, trigger: #{trigger}" target = project.targets.find {|target| target.source_file?(path) } or return target.type_check(target_sources: [], validate_signatures: false) case (status = target&.status) when Project::Target::TypeCheckStatus subtyping = status.subtyping source = target.source_files[path] provider = Project::CompletionProvider.new(source_text: source.content, path: path, subtyping: subtyping) items = begin provider.run(line: line, column: column) rescue Parser::SyntaxError [] end completion_items = items.map do |item| format_completion_item(item) end Steep.logger.debug "items = #{completion_items.inspect}" LSP::Interface::CompletionList.new( is_incomplete: false, items: completion_items ) end end end |
#response_to_hover(path:, line:, column:) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/steep/server/interaction_worker.rb', line 53 def response_to_hover(path:, line:, column:) Steep.logger.tagged "#response_to_hover" do Steep.logger.debug { "path=#{path}, line=#{line}, column=#{column}" } hover = Project::HoverContent.new(project: project) content = hover.content_for(path: path, line: line+1, column: column+1) if content range = content.location.yield_self do |location| start_position = { line: location.line - 1, character: location.column } end_position = { line: location.last_line - 1, character: location.last_column } { start: start_position, end: end_position } end LSP::Interface::Hover.new( contents: { kind: "markdown", value: format_hover(content) }, range: range ) end rescue Typing::UnknownNodeError => exn Steep.log_error exn, message: "Failed to compute hover: #{exn.inspect}" nil end end |