Skip to content

Commit b384cd5

Browse files
author
breandan
committed
Add more features to IDEService API contract
1 parent 55302f4 commit b384cd5

4 files changed

Lines changed: 49 additions & 71 deletions

File tree

src/com/jetbrains/idear/asr/ASRControlLoop.java

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,21 @@
2222
import java.awt.*;
2323
import java.awt.event.KeyEvent;
2424
import java.io.IOException;
25-
import java.util.function.Function;
2625
import java.util.logging.Level;
2726
import java.util.logging.Logger;
2827

2928
/**
3029
* Created by breandan on 10/23/2015.
3130
*/
3231
public class ASRControlLoop implements Runnable {
32+
private final IDEService ideService;
3333
private CustomLiveSpeechRecognizer recognizer;
3434

3535
public ASRControlLoop(CustomLiveSpeechRecognizer recognizer) {
3636
// Start-up recognition facilities
3737
this.recognizer = recognizer;
38+
39+
this.ideService = ServiceManager.getService(IDEService.class);
3840
}
3941

4042
private static final Logger logger = Logger.getLogger(ASRControlLoop.class.getSimpleName());
@@ -81,7 +83,7 @@ public void run() {
8183
if (result.equals(HI_IDEA)) {
8284
// Greet invoker
8385
say("Hi");
84-
invokeAction("Idear.Start");
86+
ideService.invokeAction("Idear.Start");
8587
}
8688
} else if (ListeningState.isActive()) {
8789
logger.log(Level.INFO, "Recognized: " + result);
@@ -112,19 +114,19 @@ private void applyAction(String c) {
112114
say("Hi, again!");
113115
} else if (c.startsWith(OPEN)) {
114116
if (c.endsWith(SETTINGS)) {
115-
invokeAction(IdeActions.ACTION_SHOW_SETTINGS);
117+
ideService.invokeAction(IdeActions.ACTION_SHOW_SETTINGS);
116118
} else if (c.endsWith(RECENT)) {
117-
invokeAction(IdeActions.ACTION_RECENT_FILES);
119+
ideService.invokeAction(IdeActions.ACTION_RECENT_FILES);
118120
} else if (c.endsWith(TERMINAL)) {
119121
pressKeystroke(KeyEvent.VK_ALT, KeyEvent.VK_F12);
120122
}
121123
} else if (c.startsWith(NAVIGATE)) {
122-
invokeAction("GotoDeclaration");
124+
ideService.invokeAction("GotoDeclaration");
123125
} else if (c.startsWith(EXECUTE)) {
124-
invokeAction("Run");
126+
ideService.invokeAction("Run");
125127
} else if (c.equals(WHERE_AM_I)) {
126128
// TODO(kudinkin): extract to action
127-
invokeAction("Idear.WhereAmI");
129+
ideService.invokeAction("Idear.WhereAmI");
128130
} else if (c.startsWith("focus")) {
129131
if (c.endsWith("editor")) {
130132
pressKeystroke(KeyEvent.VK_ESCAPE);
@@ -205,7 +207,7 @@ private void applyAction(String c) {
205207

206208
private void pressKeystroke(final int... keys) {
207209
ServiceManager.getService(IDEService.class)
208-
.pressKeystroke();
210+
.pressKeystroke(keys);
209211
}
210212

211213
private void run(SurroundWithNoNullCheckRecognizer rec, String c, DataContext dataContext) {
@@ -262,7 +264,7 @@ private void fireVoiceCommand() {
262264
// Notify of successful proceed
263265
beep();
264266

265-
invokeAction(
267+
ideService.invokeAction(
266268
"Idear.VoiceAction",
267269
dataContext -> new AnActionEvent(
268270
null,
@@ -311,30 +313,6 @@ private void pauseSpeech() {
311313
}
312314
}
313315

314-
315-
private void invokeAction(final String action) {
316-
invokeAction(
317-
action,
318-
dataContext ->
319-
new AnActionEvent(null,
320-
dataContext,
321-
ActionPlaces.UNKNOWN,
322-
new Presentation(),
323-
ActionManager.getInstance(),
324-
0
325-
)
326-
);
327-
}
328-
329-
private void invokeAction(String action, Function<DataContext, AnActionEvent> actionFactory) {
330-
DataManager.getInstance().getDataContextFromFocus().doWhenDone(
331-
(Consumer<DataContext>) dataContext -> EventQueue.invokeLater(() -> {
332-
AnAction anAction = ActionManager.getInstance().getAction(action);
333-
anAction.actionPerformed(actionFactory.apply(dataContext));
334-
})
335-
);
336-
}
337-
338316
// Helpers
339317

340318
public synchronized void say(String something) {

src/com/jetbrains/idear/ide/IDEService.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,22 @@
1111
import com.intellij.util.Consumer;
1212
*/
1313

14+
import com.intellij.openapi.actionSystem.AnActionEvent;
15+
import com.intellij.openapi.actionSystem.DataContext;
16+
17+
import java.util.function.Function;
18+
1419
/**
1520
* Created by breandan on 10/23/2015.
1621
*/
1722
public interface IDEService {
1823
void init();
24+
1925
void dispose();
26+
2027
void pressKeystroke(final int... keys);
2128

29+
void invokeAction(final String action);
30+
31+
void invokeAction(String action, Function<DataContext, AnActionEvent> actionFactory);
2232
}

src/com/jetbrains/idear/ide/IDEServiceImpl.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package com.jetbrains.idear.ide;
22

3+
import com.intellij.ide.DataManager;
4+
import com.intellij.openapi.actionSystem.*;
5+
import com.intellij.util.Consumer;
6+
37
import java.awt.*;
8+
import java.util.function.Function;
49
import java.util.logging.Logger;
510

611
/**
@@ -12,20 +17,41 @@ public class IDEServiceImpl implements IDEService {
1217
private Robot robot;
1318

1419
public void init() {
15-
1620
try {
1721
robot = new Robot();
1822
} catch (AWTException e) {
1923
e.printStackTrace();
2024
}
21-
2225
}
2326

2427
@Override
2528
public void dispose() {
2629

2730
}
2831

32+
public void invokeAction(final String action) {
33+
invokeAction(
34+
action,
35+
dataContext ->
36+
new AnActionEvent(null,
37+
dataContext,
38+
ActionPlaces.UNKNOWN,
39+
new Presentation(),
40+
ActionManager.getInstance(),
41+
0
42+
)
43+
);
44+
}
45+
46+
public void invokeAction(String action, Function<DataContext, AnActionEvent> actionFactory) {
47+
DataManager.getInstance().getDataContextFromFocus().doWhenDone(
48+
(Consumer<DataContext>) dataContext -> EventQueue.invokeLater(() -> {
49+
AnAction anAction = ActionManager.getInstance().getAction(action);
50+
anAction.actionPerformed(actionFactory.apply(dataContext));
51+
})
52+
);
53+
}
54+
2955
public void pressKeystroke(final int... keys) {
3056
for (int key : keys) {
3157
robot.keyPress(key);

src/com/johnlindquist/acejump/AceFinder.kt

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -144,42 +144,6 @@ public class AceFinder(val project: Project, val document: DocumentImpl, val edi
144144
});
145145
}
146146

147-
fun findAllVisible(): List<Int> {
148-
//System.out.println("----- findAllVisible");
149-
val visualLineAtTopOfScreen = EditorHelper.getVisualLineAtTopOfScreen(editor)
150-
val firstLine = EditorHelper.visualLineToLogicalLine(editor, visualLineAtTopOfScreen)
151-
var offset = EditorHelper.getLineStartOffset(editor, firstLine)
152-
153-
val height = EditorHelper.getScreenHeight(editor)
154-
val top = EditorHelper.getVisualLineAtTopOfScreen(editor)
155-
156-
var lastLine = top + height
157-
lastLine = EditorHelper.visualLineToLogicalLine(editor, lastLine)
158-
159-
var endOffset = EditorHelper.normalizeOffset(editor, lastLine, EditorHelper.getLineEndOffset(editor, lastLine, true), true)
160-
var text = document.getCharsSequence().toString().get(offset, endOffset)
161-
var offsets = ArrayList<Int>()
162-
163-
var foundOffset = 0
164-
while (0 < text.length) {
165-
var result = findManager.findString(text, foundOffset, findModel, virtualFile);
166-
if (!result.isStringFound()) {
167-
//System.out.println(findModel.getStringToFind() + ": not found");
168-
break;
169-
}
170-
var resultOffset: Int
171-
if (getEndOffset) {
172-
resultOffset = result.getEndOffset() - 1;
173-
} else {
174-
resultOffset = result.getStartOffset();
175-
}
176-
offsets.add(resultOffset + offset + customOffset);
177-
foundOffset = result.getEndOffset();
178-
}
179-
180-
return offsets;
181-
}
182-
183147
public fun expandResults() {
184148
startResult += allowedCount
185149
endResult += allowedCount

0 commit comments

Comments
 (0)