This repository was archived by the owner on Apr 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 299
/
Copy pathLanguageEditorIntegration.ts
172 lines (145 loc) · 5.55 KB
/
LanguageEditorIntegration.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
* LanguageEditorIntegration
*
* Responsible for listening to editor events,
* and hooking up the language service functionality.
*/
import { Store, Unsubscribe } from "redux"
import * as Oni from "oni-api"
import * as OniTypes from "oni-types"
import * as types from "vscode-languageserver-types"
import { Configuration } from "./../Configuration"
import { LanguageManager } from "./LanguageManager"
import { createStore, DefaultLanguageState, ILanguageState } from "./LanguageStore"
import {
IDefinitionRequestor,
IDefinitionResult,
LanguageServiceDefinitionRequestor,
} from "./DefinitionRequestor"
import { IHoverRequestor, IHoverResult, LanguageServiceHoverRequestor } from "./HoverRequestor"
export class LanguageEditorIntegration implements OniTypes.IDisposable {
private _subscriptions: OniTypes.IDisposable[] = []
private _store: Store<ILanguageState>
private _storeUnsubscribe: Unsubscribe = null
private _lastState: ILanguageState = DefaultLanguageState
private _onShowDefinition: OniTypes.Event<IDefinitionResult> = new OniTypes.Event<
IDefinitionResult
>("LanguageEditorIntegration::onShowDefinition")
private _onHideDefinition: OniTypes.Event<void> = new OniTypes.Event<void>(
"LanguageEditorIntegration::onHideDefinition",
)
private _onShowHover: OniTypes.Event<IHoverResult> = new OniTypes.Event<IHoverResult>(
"LanguageEditorIntegration::onShowHover",
)
private _onHideHover: OniTypes.Event<void> = new OniTypes.Event<void>(
"LanguageEditorIntegration::onHideHover",
)
public get onShowDefinition(): OniTypes.IEvent<IDefinitionResult> {
return this._onShowDefinition
}
public get onHideDefinition(): OniTypes.IEvent<void> {
return this._onHideDefinition
}
public get onShowHover(): OniTypes.IEvent<IHoverResult> {
return this._onShowHover
}
public get onHideHover(): OniTypes.IEvent<void> {
return this._onHideHover
}
constructor(
private _editor: Oni.Editor,
private _configuration: Configuration,
private _languageManager?: LanguageManager,
private _definitionRequestor?: IDefinitionRequestor,
private _hoverRequestor?: IHoverRequestor,
) {
this._definitionRequestor =
this._definitionRequestor ||
new LanguageServiceDefinitionRequestor(this._languageManager, this._editor)
this._hoverRequestor =
this._hoverRequestor || new LanguageServiceHoverRequestor(this._languageManager)
this._store = createStore(
this._configuration,
this._hoverRequestor,
this._definitionRequestor,
)
const sub1 = this._editor.onModeChanged.subscribe((newMode: string) => {
this._store.dispatch({
type: "MODE_CHANGED",
mode: newMode,
})
})
const sub2 = this._editor.onBufferEnter.subscribe(
(bufferEvent: Oni.EditorBufferEventArgs) => {
this._store.dispatch({
type: "BUFFER_ENTER",
filePath: bufferEvent.filePath,
language: bufferEvent.language,
})
},
)
const sub3 = this._editor.onCursorMoved.subscribe((cursorMoveEvent: Oni.Cursor) => {
this._store.dispatch({
type: "CURSOR_MOVED",
line: cursorMoveEvent.line,
column: cursorMoveEvent.column,
})
})
this._storeUnsubscribe = this._store.subscribe(() =>
this._onStateUpdate(this._store.getState()),
)
this._subscriptions = [sub1, sub2, sub3]
}
// Explicit gesture to show hover - ignores the setting
public showHover(): void {
const state = this._store.getState()
this._store.dispatch({
type: "HOVER_QUERY",
location: {
filePath: state.activeBuffer.filePath,
language: state.activeBuffer.language,
line: state.cursor.line,
column: state.cursor.column,
},
})
}
public dispose(): void {
if (this._subscriptions && this._subscriptions.length) {
this._subscriptions.forEach(disposable => disposable.dispose())
this._subscriptions = null
}
if (this._storeUnsubscribe) {
this._storeUnsubscribe()
this._storeUnsubscribe = null
}
}
private _onStateUpdate(newState: ILanguageState): void {
const newLocationResult = getLocationFromState(newState)
const lastLocationResult = getLocationFromState(this._lastState)
if (newLocationResult && !lastLocationResult) {
this._onShowDefinition.dispatch(newState.definitionResult.result)
}
if (!newLocationResult && lastLocationResult) {
this._onHideDefinition.dispatch()
}
if (newState.hoverResult.result && !this._lastState.hoverResult.result) {
this._onShowHover.dispatch(newState.hoverResult.result)
}
if (!newState.hoverResult.result && this._lastState.hoverResult.result) {
this._onHideHover.dispatch()
}
this._lastState = newState
}
}
const getLocationFromState = (state: ILanguageState): types.Location => {
if (
state &&
state.definitionResult &&
state.definitionResult.result &&
state.definitionResult.result.location
) {
return state.definitionResult.result.location
} else {
return null
}
}