Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement completion #119

Merged
merged 9 commits into from
Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/steep.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
require "steep/project/dsl"
require "steep/project/file_loader"
require "steep/project/hover_content"
require "steep/project/completion_provider"
require "steep/drivers/utils/driver_helper"
require "steep/drivers/check"
require "steep/drivers/validate"
Expand Down Expand Up @@ -114,4 +115,12 @@ def self.log_output=(output)

@logger = nil
self.log_output = STDERR

def self.measure(message)
start = Time.now
yield.tap do
time = Time.now - start
self.logger.info "#{message} took #{time} seconds"
end
end
end
109 changes: 72 additions & 37 deletions lib/steep/ast/types/factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,47 +187,82 @@ def params(type)
end

def method_type(method_type, self_type:)
case method_type
when Ruby::Signature::MethodType
fvs = self_type.free_variables()

type_params = []
alpha_vars = []
alpha_types = []

method_type.type_params.map do |name|
if fvs.include?(name)
type = Types::Var.fresh(name)
alpha_vars << name
alpha_types << type
type_params << type.name
else
type_params << name
end
fvs = self_type.free_variables()

type_params = []
alpha_vars = []
alpha_types = []

method_type.type_params.map do |name|
if fvs.include?(name)
type = Types::Var.fresh(name)
alpha_vars << name
alpha_types << type
type_params << type.name
else
type_params << name
end
subst = Interface::Substitution.build(alpha_vars, alpha_types)

type = Interface::MethodType.new(
type_params: type_params,
return_type: type(method_type.type.return_type).subst(subst),
params: params(method_type.type).subst(subst),
location: nil,
block: method_type.block&.yield_self do |block|
Interface::Block.new(
optional: !block.required,
type: Proc.new(params: params(block.type).subst(subst),
return_type: type(block.type.return_type).subst(subst), location: nil)
)
end
)
end
subst = Interface::Substitution.build(alpha_vars, alpha_types)

type = Interface::MethodType.new(
type_params: type_params,
return_type: type(method_type.type.return_type).subst(subst),
params: params(method_type.type).subst(subst),
location: nil,
block: method_type.block&.yield_self do |block|
Interface::Block.new(
optional: !block.required,
type: Proc.new(params: params(block.type).subst(subst),
return_type: type(block.type.return_type).subst(subst), location: nil)
)
end
)

if block_given?
yield type
else
type
end
end

def method_type_1(method_type, self_type:)
fvs = self_type.free_variables()

if block_given?
yield type
type_params = []
alpha_vars = []
alpha_types = []

method_type.type_params.map do |name|
if fvs.include?(name)
type = Ruby::Signature::Types::Variable.new(name: name, location: nil),
alpha_vars << name
alpha_types << type
type_params << type.name
else
type
type_params << name
end
when :any
:any
end
subst = Interface::Substitution.build(alpha_vars, alpha_types)

type = Ruby::Signature::MethodType.new(
type_params: type_params,
type: function_1(method_type.params.subst(subst), method_type.return_type.subst(subst)),
block: method_type.block&.yield_self do |block|
block_type = block.type.subst(subst)

Ruby::Signature::MethodType::Block.new(
type: function_1(block_type.params, block_type.return_type),
required: !block.optional
)
end,
location: nil
)

if block_given?
yield type
else
type
end
end

Expand Down
168 changes: 163 additions & 5 deletions lib/steep/drivers/langserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,50 @@ def handle_request(request)
change: LanguageServer::Protocol::Constant::TextDocumentSyncKind::FULL
),
hover_provider: true,
completion_provider: LanguageServer::Protocol::Interface::CompletionOptions.new(
trigger_characters: [".", "@"],
)
)
)

enqueue_type_check nil

when :"textDocument/completion"
Steep.logger.error request.inspect

params = request[:params]
uri = URI.parse(params[:textDocument][:uri])
path = project.relative_path(Pathname(uri.path))
target = project.targets.find {|target| target.source_file?(path) }
case (status = target&.status)
when Project::Target::TypeCheckStatus
subtyping = status.subtyping
source = target.source_files[path]

line, column = params[:position].yield_self {|hash| [hash[:line]+1, hash[:character]] }
trigger = params[:context][:triggerCharacter]

Steep.logger.error "line: #{line}, column: #{column}, trigger: #{trigger}"

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}"

yield id, LanguageServer::Protocol::Interface::CompletionList.new(
is_incomplete: false,
items: completion_items
)
end

when :"textDocument/didChange"
uri = URI.parse(request[:params][:textDocument][:uri])
path = project.relative_path(Pathname(uri.path))
Expand Down Expand Up @@ -185,13 +225,11 @@ def run_type_check()
source.errors.map {|error| diagnostic_for_type_error(error) }
when Project::SourceFile::AnnotationSyntaxErrorStatus
[diagnostics_raw(source.status.error.message, source.status.location)]
when Project::SourceFile::ParseErrorStatus
[]
when Project::SourceFile::TypeCheckErrorStatus
[]
end

report_diagnostics source.path, diagnostics
if diagnostics
report_diagnostics source.path, diagnostics
end
end
when Project::Target::SignatureSyntaxErrorStatus
Steep.logger.info { "Signature syntax error" }
Expand Down Expand Up @@ -331,6 +369,126 @@ def #{content.method_name}: #{content.method_type}
"`#{content.type}`"
end
end

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

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?(Ruby::Signature::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

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
end
end
end
Loading