Skip to content

Commit ecf705e

Browse files
authored
Merge pull request #271 from glennsarti/gh-269
(GH-269) Fix Workspace Symbol Provider
2 parents 580712f + 0d6054a commit ecf705e

File tree

13 files changed

+743
-683
lines changed

13 files changed

+743
-683
lines changed

lib/puppet-languageserver/manifest/document_symbol_provider.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def self.workspace_symbols(query, object_cache)
1616
'kind' => LSP::SymbolKind::METHOD,
1717
'location' => {
1818
'uri' => PuppetLanguageServer::UriHelper.build_file_uri(item.source),
19-
# Don't have char pos for functions so just pick extreme values
19+
# Don't have char pos for types so just pick extreme values
2020
'range' => LSP.create_range(item.line, 0, item.line, 1024)
2121
}
2222
)
@@ -38,11 +38,25 @@ def self.workspace_symbols(query, object_cache)
3838
'kind' => LSP::SymbolKind::CLASS,
3939
'location' => {
4040
'uri' => PuppetLanguageServer::UriHelper.build_file_uri(item.source),
41-
# Don't have char pos for functions so just pick extreme values
41+
# Don't have char pos for classes so just pick extreme values
4242
'range' => LSP.create_range(item.line, 0, item.line, 1024)
4343
}
4444
)
4545

46+
when PuppetLanguageServer::Sidecar::Protocol::PuppetDataType
47+
result << LSP::SymbolInformation.new(
48+
'name' => key_string,
49+
'kind' => LSP::SymbolKind::NAMESPACE,
50+
'location' => {
51+
'uri' => PuppetLanguageServer::UriHelper.build_file_uri(item.source),
52+
# Don't have char pos for data types so just pick extreme values
53+
'range' => LSP.create_range(item.line, 0, item.line, 1024)
54+
}
55+
)
56+
57+
when PuppetLanguageServer::Sidecar::Protocol::Fact
58+
# Do nothing
59+
4660
else
4761
PuppetLanguageServer.log_message(:warn, "[Manifest::DocumentSymbolProvider] Unknown object type #{item.class}")
4862
end

lib/puppet-languageserver/message_handler.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ def workspace_root_from_initialize_params(params)
383383
# We don't support multiple workspace folders yet, so just select the first one
384384
return UriHelper.uri_path(params['workspaceFolders'][0]['uri'])
385385
end
386-
return UriHelper.uri_path(params['rootUri']) if params.key?('rootUri')
386+
return UriHelper.uri_path(params['rootUri']) if params.key?('rootUri') && !params['rootUri'].nil?
387387
params['rootPath']
388388
end
389389
end

lib/puppet-languageserver/uri_helper.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
module PuppetLanguageServer
77
module UriHelper
88
def self.build_file_uri(path)
9-
'file://' + Puppet::Util.uri_encode(path.start_with?('/') ? path : '/' + path)
9+
if path.nil?
10+
nil
11+
else
12+
'file://' + Puppet::Util.uri_encode(path.start_with?('/') ? path : '/' + path)
13+
end
1014
end
1115

1216
def self.uri_path(uri_string)
13-
return nil if uri_string.nil?
14-
Puppet::Util.uri_to_path(URI(uri_string))
17+
uri_string.nil? ? nil : Puppet::Util.uri_to_path(URI(uri_string))
1518
end
1619

1720
# Compares two URIs and returns the relative path

lib/puppet_editor_services/protocol/json_rpc.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ def receive_json_message_as_string(content)
138138
def receive_json_message_as_hash(json_obj)
139139
# There's no need to convert it to an object quite yet
140140
# Need to validate that this is indeed a valid message
141+
id = json_obj[KEY_ID]
141142
unless json_obj[KEY_JSONRPC] == JSONRPC_VERSION
142143
PuppetEditorServices.log_message(:error, 'Invalid JSON RPC version')
143144
reply_error id, CODE_INVALID_REQUEST, MSG_INVALID_REQ_JSONRPC
@@ -159,7 +160,6 @@ def receive_json_message_as_hash(json_obj)
159160
end
160161
end
161162

162-
id = json_obj[KEY_ID]
163163
# Requests and Responses must have an ID that is either a string or integer
164164
if is_request || is_response
165165
unless id.is_a?(String) || id.is_a?(Integer)
@@ -197,6 +197,10 @@ def receive_json_message_as_hash(json_obj)
197197
false
198198
end
199199

200+
def reply_error(id, code, message)
201+
send_json_string ::PuppetEditorServices::Protocol::JsonRPCMessages.reply_error_by_id(id, code, message).to_json
202+
end
203+
200204
# region Server-to-Client request/response methods
201205
def send_client_request(rpc_method, params)
202206
request = ::PuppetEditorServices::Protocol::JsonRPCMessages.new_request(client_request_id!, rpc_method, params)

lib/puppet_editor_services/protocol/json_rpc_messages.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,14 @@ def self.reply_result(request, result)
169169
end
170170

171171
def self.reply_error(request, code, message)
172+
reply_error_by_id(request.id, code, message)
173+
end
174+
175+
def self.reply_error_by_id(id, code, message)
172176
# Note - Strictly speaking the error should be typed object, however as this hidden behind
173177
# this method it's easier to just pass in a known hash construct
174178
ResponseMessage.new.from_h!(
175-
'id' => request.id,
179+
'id' => id,
176180
'error' => {
177181
'code' => code,
178182
'message' => message

spec/languageserver/acceptance/end_to_end_spec.rb

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
# Format range | X | - | - |
2626
# OnType Formatting | X | - | - |
2727
# Document Symbols | X | - | - |
28-
# Workspace Symbols | - | | |
28+
# Workspace Symbols | - | | X |
2929

3030
describe 'End to End Testing' do
3131
before(:each) do
@@ -321,10 +321,23 @@ def path_to_uri(path)
321321
@client.send_data(@client.puppetfile_getdependencies_request(@client.next_seq_id, puppetfile_uri))
322322
expect(@client).to receive_message_with_request_id_within_timeout([@client.current_seq_id, 10])
323323
result = @client.data_from_request_seq_id(@client.current_seq_id)
324-
# Expect something to be returned
325-
expect(result['result']).not_to be_nil
326-
expect(result['result']['dependencies']).not_to be_nil
327-
expect(result['result']['dependencies']).not_to be_empty
324+
# Expect something to be returned
325+
expect(result['result']).not_to be_nil
326+
expect(result['result']['dependencies']).not_to be_nil
327+
expect(result['result']['dependencies']).not_to be_empty
328+
329+
# Workspace Symbols
330+
@client.send_data(@client.workspace_symbols_request(@client.next_seq_id, ''))
331+
expect(@client).to receive_message_with_request_id_within_timeout([@client.current_seq_id, 15])
332+
result = @client.data_from_request_seq_id(@client.current_seq_id)
333+
# Expect something to be returned
334+
expect(result['result']).not_to be_nil
335+
# Should contain the default puppet user class
336+
index = result['result'].find { |item| item['name'] == 'user' && item['kind'] == 6 }
337+
expect(index).not_to be_nil
338+
# Should contain a profile from the control-repo
339+
index = result['result'].find { |item| item['name'] == 'profile::editorservices' && item['kind'] == 5 }
340+
expect(index).not_to be_nil
328341

329342
# Start shutdown process
330343
@client.clear_messages!

0 commit comments

Comments
 (0)