1
1
import type * as atomIde from "atom-ide-base"
2
+ import * as linter from "atom/linter"
2
3
import LinterPushV2Adapter from "./linter-push-v2-adapter"
4
+ /* eslint-disable import/no-deprecated */
5
+ import IdeDiagnosticAdapter from "./diagnostic-adapter"
3
6
import assert = require( "assert" )
4
7
import Convert from "../convert"
5
8
import ApplyEditAdapter from "./apply-edit-adapter"
6
9
import {
7
10
CodeAction ,
8
11
CodeActionParams ,
9
12
Command ,
13
+ Diagnostic ,
10
14
LanguageClientConnection ,
11
15
ServerCapabilities ,
12
16
WorkspaceEdit ,
13
17
} from "../languageclient"
14
18
import { Range , TextEditor } from "atom"
15
- import CommandExecutionAdapter from "./command-execution-adapter"
16
19
17
20
export default class CodeActionAdapter {
18
21
/** @returns A {Boolean} indicating this adapter can adapt the server based on the given serverCapabilities. */
@@ -28,24 +31,24 @@ export default class CodeActionAdapter {
28
31
* @param serverCapabilities The {ServerCapabilities} of the language server that will be used.
29
32
* @param editor The Atom {TextEditor} containing the diagnostics.
30
33
* @param range The Atom {Range} to fetch code actions for.
31
- * @param diagnostics An {Array<atomIde$Diagnostic >} to fetch code actions for. This is typically a list of
32
- * diagnostics intersecting `range`.
34
+ * @param linterMessages An {Array<linter$Message >} to fetch code actions for. This is typically a list of messages
35
+ * intersecting `range`.
33
36
* @returns A {Promise} of an {Array} of {atomIde$CodeAction}s to display.
34
37
*/
35
38
public static async getCodeActions (
36
39
connection : LanguageClientConnection ,
37
40
serverCapabilities : ServerCapabilities ,
38
- linterAdapter : LinterPushV2Adapter | undefined ,
41
+ linterAdapter : LinterPushV2Adapter | IdeDiagnosticAdapter | undefined ,
39
42
editor : TextEditor ,
40
43
range : Range ,
41
- diagnostics : atomIde . Diagnostic [ ]
44
+ linterMessages : linter . Message [ ] | atomIde . Diagnostic [ ]
42
45
) : Promise < atomIde . CodeAction [ ] > {
43
46
if ( linterAdapter == null ) {
44
47
return [ ]
45
48
}
46
49
assert ( serverCapabilities . codeActionProvider , "Must have the textDocument/codeAction capability" )
47
50
48
- const params = CodeActionAdapter . createCodeActionParams ( linterAdapter , editor , range , diagnostics )
51
+ const params = createCodeActionParams ( linterAdapter , editor , range , linterMessages )
49
52
const actions = await connection . codeAction ( params )
50
53
if ( actions === null ) {
51
54
return [ ]
@@ -81,34 +84,44 @@ export default class CodeActionAdapter {
81
84
82
85
private static async executeCommand ( command : any , connection : LanguageClientConnection ) : Promise < void > {
83
86
if ( Command . is ( command ) ) {
84
- await CommandExecutionAdapter . executeCommand ( connection , command . command , command . arguments )
87
+ await connection . executeCommand ( {
88
+ command : command . command ,
89
+ arguments : command . arguments ,
90
+ } )
85
91
}
86
92
}
93
+ }
87
94
88
- private static createCodeActionParams (
89
- linterAdapter : LinterPushV2Adapter ,
90
- editor : TextEditor ,
91
- range : Range ,
92
- diagnostics : atomIde . Diagnostic [ ]
93
- ) : CodeActionParams {
94
- return {
95
- textDocument : Convert . editorToTextDocumentIdentifier ( editor ) ,
96
- range : Convert . atomRangeToLSRange ( range ) ,
97
- context : {
98
- diagnostics : diagnostics . map ( ( diagnostic ) => {
99
- // Retrieve the stored diagnostic code if it exists.
100
- // Until the Linter API provides a place to store the code,
101
- // there's no real way for the code actions API to give it back to us.
102
- const converted = Convert . atomIdeDiagnosticToLSDiagnostic ( diagnostic )
103
- if ( diagnostic . range != null && diagnostic . text != null ) {
104
- const code = linterAdapter . getDiagnosticCode ( editor , diagnostic . range , diagnostic . text )
105
- if ( code != null ) {
106
- converted . code = code
107
- }
108
- }
109
- return converted
110
- } ) ,
111
- } ,
112
- }
95
+ function createCodeActionParams (
96
+ linterAdapter : LinterPushV2Adapter | IdeDiagnosticAdapter ,
97
+ editor : TextEditor ,
98
+ range : Range ,
99
+ linterMessages : linter . Message [ ] | atomIde . Diagnostic [ ]
100
+ ) : CodeActionParams {
101
+ let diagnostics : Diagnostic [ ]
102
+ if ( linterMessages . length === 0 ) {
103
+ diagnostics = [ ]
104
+ } else {
105
+ // TODO compile time dispatch using function names
106
+ diagnostics = areLinterMessages ( linterMessages )
107
+ ? linterAdapter . getLSDiagnosticsForMessages ( linterMessages as linter . Message [ ] )
108
+ : ( linterAdapter as IdeDiagnosticAdapter ) . getLSDiagnosticsForIdeDiagnostics (
109
+ linterMessages as atomIde . Diagnostic [ ] ,
110
+ editor
111
+ )
112
+ }
113
+ return {
114
+ textDocument : Convert . editorToTextDocumentIdentifier ( editor ) ,
115
+ range : Convert . atomRangeToLSRange ( range ) ,
116
+ context : {
117
+ diagnostics,
118
+ } ,
119
+ }
120
+ }
121
+
122
+ function areLinterMessages ( linterMessages : linter . Message [ ] | atomIde . Diagnostic [ ] ) : boolean {
123
+ if ( "excerpt" in linterMessages [ 0 ] ) {
124
+ return true
113
125
}
126
+ return false
114
127
}
0 commit comments