diff --git a/lib/puppet-languageserver/message_router.rb b/lib/puppet-languageserver/message_router.rb index 59a7db06..5d47f697 100644 --- a/lib/puppet-languageserver/message_router.rb +++ b/lib/puppet-languageserver/message_router.rb @@ -156,8 +156,11 @@ def receive_request(request) case documents.document_type(file_uri) when :manifest request.reply_result(PuppetLanguageServer::Manifest::DocumentSymbolProvider.extract_document_symbols(content, :tasks_mode => PuppetLanguageServer::DocumentStore.module_plan_file?(file_uri))) + when :puppetfile + result = PuppetLanguageServer::Puppetfile::DocumentSymbolProvider.extract_document_symbols(content) + request.reply_result(result) else - raise "Unable to provide definition on #{file_uri}" + raise "Unable to provide document symbols on #{file_uri}" end rescue StandardError => exception PuppetLanguageServer.log_message(:error, "(textDocument/documentSymbol) #{exception}") diff --git a/lib/puppet-languageserver/providers.rb b/lib/puppet-languageserver/providers.rb index 9c88636c..6461afe1 100644 --- a/lib/puppet-languageserver/providers.rb +++ b/lib/puppet-languageserver/providers.rb @@ -12,6 +12,7 @@ puppetfile/r10k/module/git puppetfile/r10k/module/svn puppetfile/r10k/puppetfile + puppetfile/document_symbol_provider puppetfile/validation_provider ].each do |lib| begin diff --git a/lib/puppet-languageserver/puppetfile/document_symbol_provider.rb b/lib/puppet-languageserver/puppetfile/document_symbol_provider.rb new file mode 100644 index 00000000..5d30aec8 --- /dev/null +++ b/lib/puppet-languageserver/puppetfile/document_symbol_provider.rb @@ -0,0 +1,44 @@ +module PuppetLanguageServer + module Puppetfile + module DocumentSymbolProvider + def self.max_line_length + # TODO: ... need to figure out the actual line length + 1000 + end + + # def self.workspace_symbols(query) + # query = '' if query.nil? + # result = [] + # result + # end + + def self.extract_document_symbols(content) + symbols = [] + + puppetfile = nil + begin + puppetfile = PuppetLanguageServer::Puppetfile::R10K::Puppetfile.new + puppetfile.load!(content) + rescue StandardError, SyntaxError, LoadError => _detail + return symbols + end + return symbols if puppetfile.nil? + + puppetfile.modules.each do |mod| + next if mod.properties[:type] == :invalid + + symbols.push(LanguageServer::DocumentSymbol.create( + 'name' => mod.name, + 'kind' => LanguageServer::SYMBOLKIND_FILE, + 'detail' => mod.title, + 'range' => [mod.puppetfile_line_number, 0, mod.puppetfile_line_number, max_line_length], + 'selectionRange' => [mod.puppetfile_line_number, 0, mod.puppetfile_line_number, max_line_length], + 'children' => [] + )) + end + + symbols + end + end + end +end