Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

Commit

Permalink
Adds documentation resolve to method completion (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
laginha87 authored and faustinoaq committed Mar 7, 2018
1 parent 5c983db commit ca14fd8
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 5 deletions.
1 change: 1 addition & 0 deletions spec/fixtures/completion/tree.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Node
def initialize(@value : Char)
end

# Adds a node to the tree
def add(x)
if x < @value
if left = @left
Expand Down
11 changes: 11 additions & 0 deletions spec/scry/completion_resolver_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,16 @@ module Scry
doc = results.documentation.as(MarkupContent)
doc.value.should eq("```crystal\n# taken from https://raw.githubusercontent.com/crystal-lang/crystal/master/samples/tree.cr\nclass Node\n @left : self?\n @right : self?\n\n```")
end

it "handles method completions" do
tree_path = File.expand_path("spec/fixtures/completion/tree.cr")
context = MethodCallContextData.new(tree_path, ":10:3")
completion_item = CompletionItem.new(label = "mock", kind = CompletionItemKind::Text, detail = "mock detail", data = context, documentation = nil)
completion_resolver = CompletionResolver.new(1, completion_item)

results = completion_resolver.run.as(CompletionItem)
doc = results.documentation.as(MarkupContent)
doc.value.should eq(" Adds a node to the tree")
end
end
end
6 changes: 4 additions & 2 deletions src/scry/completion/method_call_context.cr
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ module Scry::Completion

def to_completion_items(results : Array(MethodDBEntry))
results.map do |res|
label = res.name
CompletionItem.new(label, CompletionItemKind::Method, label, nil)
CompletionItem.new(res.name,
CompletionItemKind::Method,
"#{res.name}#{res.signature}",
MethodCallContextData.new(res.file_path, res.location))
end
end
end
Expand Down
6 changes: 4 additions & 2 deletions src/scry/completion/method_db.cr
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ module Scry::Completion
struct MethodDBEntry
property name : String
property signature : String
property file_path : String
property location : String

def initialize(@name, @signature)
def initialize(@name, @signature, @file_path, @location)
end
end

Expand All @@ -60,7 +62,7 @@ module Scry::Completion
method_receiver = node.receiver ? "#{@class_queue.last}.class" : @class_queue.last
end
signature = "(#{node.args.map(&.to_s).join(", ")}) : #{return_type || node.return_type.to_s}"
@classes[method_receiver] << MethodDBEntry.new(name, signature)
@classes[method_receiver] << MethodDBEntry.new(name, signature, @file, node.location.to_s)
false
end

Expand Down
6 changes: 6 additions & 0 deletions src/scry/completion_resolver.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ module Scry
doc = file.each_line.first(5).join("\n")
@completionItem.documentation = MarkupContent.new("markdown", "```crystal\n#{doc}\n```")
@completionItem
when MethodCallContextData
file = File.new data.path
line = data.location.not_nil!.split(":")[1].to_i
lines = file.each_line.first(line-1).to_a.reverse.take_while(&.match /^\s*#/).map{|e| e.gsub(/^\s*#/, "") }.reverse.join("\n")
@completionItem.documentation = MarkupContent.new("markdown", lines)
@completionItem
else
@completionItem
end
Expand Down
12 changes: 11 additions & 1 deletion src/scry/protocol/completion_item.cr
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,18 @@ module Scry
@require_module_context = true
end
end
struct MethodCallContextData
JSON.mapping({
method_completion_context: Bool,
path: String,
location: String
})
def initialize(@path, @location)
@method_completion_context = true
end
end

alias CompletionItemData = RequireModuleContextData | Nil
alias CompletionItemData = RequireModuleContextData | MethodCallContextData

struct CompletionItem
JSON.mapping({
Expand Down

0 comments on commit ca14fd8

Please sign in to comment.