forked from puppetlabs/puppet-editor-services
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(puppetlabsGH-33) Add Code Action provider for ignoring Lint Errors
TODO
- Loading branch information
1 parent
99fce1d
commit a35ea3b
Showing
7 changed files
with
264 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
module LanguageServer | ||
# /** | ||
# * Params for the CodeActionRequest | ||
# */ | ||
# interface CodeActionParams { | ||
# /** | ||
# * The document in which the command was invoked. | ||
# */ | ||
# textDocument: TextDocumentIdentifier; | ||
|
||
# /** | ||
# * The range for which the command was invoked. | ||
# */ | ||
# range: Range; | ||
|
||
# /** | ||
# * Context carrying additional information. | ||
# */ | ||
# context: CodeActionContext; | ||
# } | ||
module CodeActionRequest | ||
def self.create(options) | ||
result = {} | ||
raise('textDocument is a required field for CodeActionRequest') if options['textDocument'].nil? | ||
raise('range is a required field for CodeActionRequest') if options['range'].nil? | ||
raise('context is a required field for CodeActionRequest') if options['context'].nil? | ||
|
||
result['textDocument'] = options['textDocument'] | ||
result['range'] = options['range'] | ||
result['context'] = CodeActionContext.create(options['context']) | ||
|
||
result | ||
end | ||
end | ||
|
||
# /** | ||
# * Contains additional diagnostic information about the context in which | ||
# * a code action is run. | ||
# */ | ||
# interface CodeActionContext { | ||
# /** | ||
# * An array of diagnostics. | ||
# */ | ||
# diagnostics: Diagnostic[]; | ||
|
||
# /** | ||
# * Requested kind of actions to return. | ||
# * | ||
# * Actions not of this kind are filtered out by the client before being shown. So servers | ||
# * can omit computing them. | ||
# */ | ||
# only?: CodeActionKind[]; | ||
# } | ||
module CodeActionContext | ||
def self.create(options) | ||
result = {} | ||
raise('diagnostics is a required field for CodeActionContext') if options['diagnostics'].nil? | ||
|
||
result['diagnostics'] = [] | ||
|
||
options['diagnostics'].each do |diag| | ||
# TODO: Should really ask Diagnostic.create to create the object | ||
result['diagnostics'] << diag | ||
end | ||
result['only'] = options['only'] unless options['only'].nil? | ||
|
||
result | ||
end | ||
end | ||
|
||
# /** | ||
# * A code action represents a change that can be performed in code, e.g. to fix a problem or | ||
# * to refactor code. | ||
# * | ||
# * A CodeAction must set either `edit` and/or a `command`. If both are supplied, the `edit` is applied first, then the `command` is executed. | ||
# */ | ||
# export interface CodeAction { | ||
|
||
# /** | ||
# * A short, human-readable, title for this code action. | ||
# */ | ||
# title: string; | ||
|
||
# /** | ||
# * The kind of the code action. | ||
# * | ||
# * Used to filter code actions. | ||
# */ | ||
# kind?: CodeActionKind; | ||
|
||
# /** | ||
# * The diagnostics that this code action resolves. | ||
# */ | ||
# diagnostics?: Diagnostic[]; | ||
|
||
# /** | ||
# * The workspace edit this code action performs. | ||
# */ | ||
# edit?: WorkspaceEdit; | ||
|
||
# /** | ||
# * A command this code action executes. If a code action | ||
# * provides an edit and a command, first the edit is | ||
# * executed and then the command. | ||
# */ | ||
# command?: Command; | ||
# } | ||
module CodeAction | ||
def self.create(options) | ||
result = {} | ||
raise('title is a required field for CodeAction') if options['title'].nil? | ||
|
||
result['title'] = options['title'] | ||
result['kind'] = options['kind'] unless options['kind'].nil? | ||
result['diagnostics'] = options['diagnostics'] unless options['diagnostics'].nil? | ||
result['edit'] = options['edit'] unless options['edit'].nil? | ||
result['command'] = options['command'] unless options['command'].nil? | ||
|
||
result | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
lib/puppet-languageserver/manifest/code_action_provider.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
module PuppetLanguageServer | ||
module Manifest | ||
module CodeActionProvider | ||
def self.provide_actions(code_action_request) | ||
# We only provide actions on diagnostics | ||
return nil if code_action_request['context']['diagnostics'].empty? | ||
file_uri = code_action_request['textDocument']['uri'] | ||
content = PuppetLanguageServer::DocumentStore.document(file_uri) | ||
# We only providea actions on files that are being edited | ||
return nil if content.nil? | ||
content_version = PuppetLanguageServer::DocumentStore.document_version(file_uri) | ||
# Extract the per line information | ||
content_lines = content.lines | ||
|
||
result = [] | ||
code_action_request['context']['diagnostics'].each do |diag| | ||
text_change = " # lint:ignore:#{diag['code']}" | ||
diag_start_line = diag['range']['start']['line'] | ||
result << LanguageServer::CodeAction.create( | ||
'title' => " Ignore '#{diag['message']}'", | ||
'edit' => { | ||
'documentChanges' => [{ | ||
'textDocument' => { | ||
'uri' => file_uri, | ||
'version' => content_version | ||
}, | ||
'edits' => [ | ||
{ | ||
'range' => { | ||
'start' => { 'line' => diag_start_line, 'character' => line_length(content_lines, diag_start_line) }, | ||
'end' => { 'line' => diag_start_line, 'character' => line_length(content_lines, diag_start_line) + 1 } | ||
}, | ||
'newText' => text_change | ||
} | ||
] | ||
}] | ||
}, | ||
'kind' => LanguageServer::CODEACTIONKIND_REFACTORINLINE, | ||
'diagnostics' => [diag] | ||
) | ||
end | ||
|
||
result | ||
end | ||
|
||
def self.line_length(content_lines, line_number) | ||
content_lines[line_number].chomp.length | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters