Skip to content

Commit 387f9e7

Browse files
michael-prattjzheaux
authored andcommitted
BAEL-1990: IntelliJ plugin example (eugenp#4942)
* Initial revision * Add actions and plugin config * Add language tag to search action * Cleanup before submitting
1 parent 62fa3aa commit 387f9e7

50 files changed

Lines changed: 134 additions & 548 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<idea-plugin>
2+
<id>com.baeldung.intellij.stackoverflowplugin</id>
3+
<name>Stack Overflow Plugin for IntelliJ</name>
4+
<version>1.0</version>
5+
<vendor email="eugene@baeldung.com" url="http://www.baeldung.com">Baeldung</vendor>
6+
7+
<description><![CDATA[
8+
Plugin to help search Stack Overflow from inside IntelliJ
9+
]]></description>
10+
11+
<change-notes><![CDATA[
12+
<ul>
13+
<li>1.0 - Initial release</li>
14+
</ul>
15+
]]>
16+
</change-notes>
17+
18+
<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description -->
19+
<idea-version since-build="173.0"/>
20+
21+
<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html
22+
on how to target different products -->
23+
<!-- uncomment to enable plugin in all products
24+
<depends>com.intellij.modules.lang</depends>
25+
-->
26+
27+
<extensions defaultExtensionNs="com.intellij">
28+
<!-- Add your extensions here -->
29+
</extensions>
30+
31+
<actions>
32+
33+
<!-- Add Ask question action to Tools Menu -->
34+
<action id="StackOverflow.AskQuestion.ToolsMenu"
35+
class="com.baeldung.intellij.stackoverflowplugin.AskQuestionAction"
36+
text="Ask Question on Stack Overflow"
37+
icon="so-icon-16x16.png"
38+
description="Ask a Question on Stack Overflow">
39+
<add-to-group group-id="ToolsMenu" anchor="last"/>
40+
</action>
41+
42+
<!-- Add action to search Stack Overflow from file editor -->
43+
<action id="StackOverflow.Search.Editor"
44+
class="com.baeldung.intellij.stackoverflowplugin.SearchAction"
45+
text="Search on Stack Overflow"
46+
icon="so-icon-16x16.png"
47+
description="Search on Stack Overflow">
48+
<add-to-group group-id="EditorPopupMenu" anchor="last"/>
49+
</action>
50+
51+
<!-- Add action to search Stack Overflow from console editor -->
52+
<action id="StackOverflow.Search.Console"
53+
class="com.baeldung.intellij.stackoverflowplugin.SearchAction"
54+
text="Search on Stack Overflow"
55+
icon="so-icon-16x16.png"
56+
description="Search on Stack Overflow">
57+
<add-to-group group-id="ConsoleEditorPopupMenu" anchor="last"/>
58+
</action>
59+
60+
</actions>
61+
62+
</idea-plugin>
454 Bytes
Loading
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.intellij.stackoverflowplugin;
2+
3+
import com.intellij.ide.BrowserUtil;
4+
import com.intellij.openapi.actionSystem.AnAction;
5+
import com.intellij.openapi.actionSystem.AnActionEvent;
6+
7+
public class AskQuestionAction extends AnAction
8+
{
9+
@Override
10+
public void actionPerformed(AnActionEvent e)
11+
{
12+
BrowserUtil.browse("https://stackoverflow.com/questions/ask");
13+
}
14+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.intellij.stackoverflowplugin;
2+
3+
import com.intellij.ide.BrowserUtil;
4+
import com.intellij.lang.Language;
5+
import com.intellij.openapi.actionSystem.ActionManager;
6+
import com.intellij.openapi.actionSystem.AnAction;
7+
import com.intellij.openapi.actionSystem.AnActionEvent;
8+
import com.intellij.openapi.actionSystem.CommonDataKeys;
9+
import com.intellij.openapi.editor.CaretModel;
10+
import com.intellij.openapi.editor.Editor;
11+
import com.intellij.psi.PsiFile;
12+
13+
public class SearchAction extends AnAction
14+
{
15+
/**
16+
* Convert selected text to a URL friendly string.
17+
* @param e
18+
*/
19+
@Override
20+
public void actionPerformed(AnActionEvent e)
21+
{
22+
final Editor editor = e.getRequiredData(CommonDataKeys.EDITOR);
23+
CaretModel caretModel = editor.getCaretModel();
24+
25+
// For searches from the editor, we should also get file type information
26+
// to help add scope to the search using the Stack overflow search syntax.
27+
//
28+
// https://stackoverflow.com/help/searching
29+
30+
String languageTag = "";
31+
PsiFile file = e.getData(CommonDataKeys.PSI_FILE);
32+
if(file != null)
33+
{
34+
Language lang = e.getData(CommonDataKeys.PSI_FILE).getLanguage();
35+
languageTag = "+[" + lang.getDisplayName().toLowerCase() + "]";
36+
}
37+
38+
// The update method below is only called periodically so need
39+
// to be careful to check for selected text
40+
if(caretModel.getCurrentCaret().hasSelection())
41+
{
42+
String query = caretModel.getCurrentCaret().getSelectedText().replace(' ', '+') + languageTag;
43+
BrowserUtil.browse("https://stackoverflow.com/search?q=" + query);
44+
}
45+
}
46+
47+
/**
48+
* Only make this action visible when text is selected.
49+
* @param e
50+
*/
51+
@Override
52+
public void update(AnActionEvent e)
53+
{
54+
final Editor editor = e.getRequiredData(CommonDataKeys.EDITOR);
55+
CaretModel caretModel = editor.getCaretModel();
56+
e.getPresentation().setEnabledAndVisible(caretModel.getCurrentCaret().hasSelection());
57+
}
58+
}

‎out/production/generated-sources8/src.main.resources.reladomo.ReladomoClassList.xml.log‎

Lines changed: 0 additions & 2 deletions
This file was deleted.

‎out/production/introduction/views/index.scala.html‎

Lines changed: 0 additions & 20 deletions
This file was deleted.

‎out/production/introduction/views/main.scala.html‎

Lines changed: 0 additions & 23 deletions
This file was deleted.

‎out/production/main122/.gitignore‎

Lines changed: 0 additions & 13 deletions
This file was deleted.

‎out/production/main151/com/baeldung/.gitignore‎

Lines changed: 0 additions & 13 deletions
This file was deleted.

‎out/production/main151/com/baeldung/README.md‎

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)