Skip to content
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
7 changes: 5 additions & 2 deletions lib/ruby_lsp/listeners/completion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -445,11 +445,14 @@ def complete_require_relative(node)
return unless arguments_node

path_node_to_complete = arguments_node.arguments.first

return unless path_node_to_complete.is_a?(Prism::StringNode)

origin_dir = Pathname.new(@uri.to_standardized_path).dirname
# If the file is unsaved (e.g.: untitled:Untitled-1), we can't provide relative completion as we don't know
# where the user intends to save it
full_path = @uri.to_standardized_path
return unless full_path

origin_dir = Pathname.new(full_path).dirname
content = path_node_to_complete.content
# if the path is not a directory, glob all possible next characters
# for example ../somethi| (where | is the cursor position)
Expand Down
19 changes: 19 additions & 0 deletions test/requests/completion_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1761,6 +1761,25 @@ def baz
end
end

def test_require_relative_returns_empty_result_for_unsaved_files
prefix = "support/"
source = <<~RUBY
require_relative "#{prefix}"
RUBY
end_char = source.rindex('"') #: as !nil

with_server(source, URI("untitled:Untitled-1")) do |server, uri|
with_file_structure(server) do
server.process_message(id: 1, method: "textDocument/completion", params: {
textDocument: { uri: uri },
position: { line: 0, character: end_char },
})

assert_empty(server.pop_response.response)
end
end
end

private

def with_file_structure(server, &block)
Expand Down
Loading