Skip to content

Commit

Permalink
(GH-66) Fix go to definition
Browse files Browse the repository at this point in the history
This commit fixes building file paths on platforms that use leading
slashes. Before the `file://` uri had too many slashes with a path that
starts with a backslash. This fix checks the string to see if it starts
with a slash and adds the appropriate amount of slashes.
  • Loading branch information
jpogran committed Oct 29, 2018
1 parent 6424f82 commit 61fcfbb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/puppet-languageserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
puppet_parser_helper
puppet_helper
facter_helper
uri_helper
puppet_monkey_patches
providers
].each do |lib|
Expand Down
4 changes: 2 additions & 2 deletions lib/puppet-languageserver/manifest/definition_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def self.type_or_class(resource_name)
item = PuppetLanguageServer::PuppetHelper.get_class(resource_name) if item.nil?
unless item.nil?
return LanguageServer::Location.create(
'uri' => 'file:///' + item.source,
'uri' => PuppetLanguageServer::UriHelper.build_file_uri(item.source),
'fromline' => item.line,
'fromchar' => 0,
'toline' => item.line,
Expand All @@ -85,7 +85,7 @@ def self.function_name(func_name)
item = PuppetLanguageServer::PuppetHelper.function(func_name)
return nil if item.nil? || item.source.nil? || item.line.nil?
LanguageServer::Location.create(
'uri' => 'file:///' + item.source,
'uri' => PuppetLanguageServer::UriHelper.build_file_uri(item.source),
'fromline' => item.line,
'fromchar' => 0,
'toline' => item.line,
Expand Down
7 changes: 7 additions & 0 deletions lib/puppet-languageserver/uri_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module PuppetLanguageServer
module UriHelper
def self.build_file_uri(path)
path.start_with?('/') ? 'file://' + path : 'file:///' + path
end
end
end
14 changes: 14 additions & 0 deletions spec/languageserver/unit/puppet-languageserver/uri_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'spec_helper'

describe 'uri_helper' do
describe '#build_file_uri' do
it 'should return /// without leading slash' do
test = PuppetLanguageServer::UriHelper.build_file_uri('C:\foo.pp')
expect(test).to eq('file:///C:\foo.pp')
end
it 'should return // with a leading slash' do
test = PuppetLanguageServer::UriHelper.build_file_uri('/opt/foo/foo.pp')
expect(test).to eq('file:///opt/foo/foo.pp')
end
end
end

0 comments on commit 61fcfbb

Please sign in to comment.