Skip to content

Commit af61569

Browse files
authored
Merge pull request #111 from austb/lint_from_module_dir
(GH-103) Parse puppet-lint.rc in module directory
2 parents 09b9b02 + b536b62 commit af61569

File tree

5 files changed

+45
-8
lines changed

5 files changed

+45
-8
lines changed

client/src/connection.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ export class ConnectionManager implements IConnectionManager {
224224

225225
if ((this.connectionConfiguration.host == undefined) || (this.connectionConfiguration.host == '')) {
226226
args.push('--ip=127.0.0.1');
227+
args.push('--local-workspace=' + vscode.workspace.textDocuments[0]);
227228
} else {
228229
args.push('--ip=' + this.connectionConfiguration.host);
229230
}

server/lib/puppet-languageserver.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ def self.parse(options)
2424
connection_timeout: 10,
2525
preload_puppet: true,
2626
debug: nil,
27-
fast_start_tcpserver: true
27+
fast_start_tcpserver: true,
28+
workspace: nil,
2829
}
2930

3031
opt_parser = OptionParser.new do |opts|
@@ -58,6 +59,10 @@ def self.parse(options)
5859
args[:fast_start_tcpserver] = false
5960
end
6061

62+
opts.on('--local-workspace=PATH', 'The workspace or file path that will be used to provide module-specific functionality. Default is no workspace path.') do |path|
63+
args[:workspace] = path
64+
end
65+
6166
opts.on('-h', '--help', 'Prints this help') do
6267
puts opts
6368
exit

server/lib/puppet-languageserver/document_validator.rb

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,43 @@
11
require 'puppet-lint'
22
module PuppetLanguageServer
33
module DocumentValidator
4-
def self.validate(content, _max_problems = 100)
4+
def self.find_module_root_from_path(path)
5+
return nil if path.nil?
6+
7+
filepath = Pathname.new(path).expand_path
8+
return nil unless filepath.exist?
9+
10+
if filepath.directory?
11+
directory = filepath
12+
else
13+
directory = filepath.dirname
14+
end
15+
16+
module_root = nil
17+
directory.ascend do |p|
18+
if p.join('metadata.json').exist?
19+
module_root = p
20+
break
21+
end
22+
end
23+
24+
module_root
25+
end
26+
27+
def self.validate(content, workspace, _max_problems = 100)
528
result = []
629
# TODO: Need to implement max_problems
730
problems = 0
831

9-
begin
10-
# TODO: load .puppet-lint.rc from the module root as well
32+
# Find module root and attempt to build PuppetLint options
33+
module_root = find_module_root_from_path(workspace)
34+
if module_root.nil?
1135
PuppetLint::OptParser.build
36+
else
37+
Dir.chdir(module_root.to_s) { PuppetLint::OptParser.build }
38+
end
39+
40+
begin
1241
linter = PuppetLint::Checks.new
1342
problems = linter.run(nil, content)
1443
unless problems.nil?

server/lib/puppet-languageserver/message_router.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ def self.document(uri)
2424
class MessageRouter < JSONRPCHandler
2525
def initialize(*options)
2626
super(*options)
27+
28+
@workspace = options.first[:workspace] unless options.compact.empty?
2729
end
2830

2931
def documents
@@ -139,7 +141,7 @@ def receive_notification(method, params)
139141
file_uri = params['textDocument']['uri']
140142
content = params['textDocument']['text']
141143
documents.set_document(file_uri, content)
142-
reply_diagnostics(file_uri, PuppetLanguageServer::DocumentValidator.validate(content))
144+
reply_diagnostics(file_uri, PuppetLanguageServer::DocumentValidator.validate(content, @workspace))
143145

144146
when 'textDocument/didClose'
145147
PuppetLanguageServer.log_message(:info, 'Received textDocument/didClose notification.')
@@ -151,7 +153,7 @@ def receive_notification(method, params)
151153
file_uri = params['textDocument']['uri']
152154
content = params['contentChanges'][0]['text'] # TODO: Bad hardcoding zero
153155
documents.set_document(file_uri, content)
154-
reply_diagnostics(file_uri, PuppetLanguageServer::DocumentValidator.validate(content))
156+
reply_diagnostics(file_uri, PuppetLanguageServer::DocumentValidator.validate(content, @workspace))
155157

156158
when 'textDocument/didSave'
157159
PuppetLanguageServer.log_message(:info, 'Received textDocument/didSave notification.')

server/spec/integration/puppet-languageserver/document_validator_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
let(:manifest) { 'user { "Bob"' }
99

1010
it "should return at least one error" do
11-
result = subject.validate(manifest)
11+
result = subject.validate(manifest, nil)
1212
expect(result.length).to be > 0
1313
end
1414
end
@@ -17,7 +17,7 @@
1717
let(:manifest) { "user { 'Bob': ensure => 'present' }" }
1818

1919
it "should return an empty array" do
20-
expect(subject.validate(manifest)).to eq([])
20+
expect(subject.validate(manifest, nil)).to eq([])
2121
end
2222
end
2323

0 commit comments

Comments
 (0)