Skip to content

Commit 5fbb5a8

Browse files
committed
feat(new-tutorial-post): 2021-06-16-intellij-plugin-search-snippets-from-ide.md
1 parent 404e9e7 commit 5fbb5a8

File tree

2 files changed

+101
-14
lines changed

2 files changed

+101
-14
lines changed

_posts/2020/2020-06-07-first-intellij-plugin.md renamed to _posts/2020/2020-06-07-first-intellij-plugin-to-save-snippets-to-codever.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
layout: post
3-
title: So, I wrote my first IntelliJ plugin to help me bookmark code
3+
title: My first IntelliJ plugin to help me save code snippets to Codever
44
description: "Shows the basic steps needed to create a simple IntelliJ plugins which eases the saving
55
of code snippets to www.codever.land"
66
author: ama
@@ -179,7 +179,6 @@ public class SaveToCodeverAction extends AnAction {
179179
return null;
180180
}
181181
}
182-
183182
}
184183
```
185184

@@ -282,42 +281,44 @@ by accessing the `getUrl()` method of the same `VirtualFile`:
282281
```
283282

284283
## Registering the action
284+
285285
There are two main ways to register an action: either by listing it in the `<actions>` section of a plugin's `plugin.xml` file, or through code.
286286

287287
In this plugin we use the configuration ``plugin.xml`` possibility:
288-
```
288+
289+
```xml
289290
<actions>
290291
<action
291-
id="BookmarsDev.Save.Editor"
292+
id="Codever.Save.Editor"
292293
class="codever.intellij.plugin.SaveToCodeverAction"
293294
text="Save to Codever"
294295
description="Save snippet to Codever"
295-
icon="SaveToCodeverPluginIcons.SAVE_TO_BOOKMARKS_DEV_ACTION">
296+
icon="SaveToCodeverPluginIcons.CODEVER_ACTION_ICON">
296297
<add-to-group group-id="EditorPopupMenu" anchor="last"/>
297298
</action>
298299
<action
299-
id="BookmarsDev.Search.Editor"
300+
id="Codever.Search.Editor"
300301
class="codever.intellij.plugin.SearchOnCodeverDialog"
301302
text="Search on Codever"
302303
description="Launches dialog to input query search on Codever"
303-
icon="SaveToCodeverPluginIcons.SAVE_TO_BOOKMARKS_DEV_ACTION">
304+
icon="SaveToCodeverPluginIcons.CODEVER_ACTION_ICON">
304305
<add-to-group group-id="EditorPopupMenu" anchor="last"/>
305306
</action>
306307
<action
307-
id="BookmarsDev.Save.Console"
308+
id="Codever.Save.Console"
308309
class="codever.intellij.plugin.SaveToCodeverAction"
309310
text="Save to Codever"
310311
description="Save snippet to Codever"
311-
icon="SaveToCodeverPluginIcons.SAVE_TO_BOOKMARKS_DEV_ACTION">
312+
icon="SaveToCodeverPluginIcons.CODEVER_ACTION_ICON">
312313
<add-to-group group-id="ConsoleEditorPopupMenu" anchor="last"/>
313314
</action>
314315
<action
315-
id="BookmarsDev.Search.Console"
316+
id="Codever.Search.Console"
316317
class="codever.intellij.plugin.SearchOnCodeverDialog"
317318
text="Search on Codever"
318319
description="Launches dialog to input query search on Codever"
319-
icon="SaveToCodeverPluginIcons.SAVE_TO_BOOKMARKS_DEV_ACTION">
320-
<add-to-group group-id="EditorPopupMenu" anchor="last"/>
320+
icon="SaveToCodeverPluginIcons.CODEVER_ACTION_ICON">
321+
<add-to-group group-id="ConsoleEditorPopupMenu" anchor="last"/>
321322
</action>
322323
</actions>
323324
```
@@ -368,15 +369,15 @@ manually:
368369
```xml
369370
<actions>
370371
<action
371-
id="BookmarsDev.Save.Editor"
372+
id="Codever.Save.Editor"
372373
class="dev.bookmarks.intellij.plugin.SaveToBookmarksDevAction"
373374
text="Save to Bookmarks.dev"
374375
description="Save code snippet to Bookmarks.dev"
375376
icon="SaveToBookmarksDevPluginIcons.SAVE_TO_BOOKMARKS_DEV_ACTION">
376377
<add-to-group group-id="EditorPopupMenu" anchor="last"/>
377378
</action>
378379
<action
379-
id="BookmarsDev.Save.Console"
380+
id="Codever.Save.Console"
380381
class="dev.bookmarks.intellij.plugin.SaveToBookmarksDevAction"
381382
text="Save to Bookmarks.dev"
382383
description="Save code snippet to Bookmarks.dev"
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
layout: post
3+
title: How to use the input dialog in an IntelliJ plugin
4+
description: "Shows how to use the IntelliJ input in a plugin to get query text to search snippets on Codever."
5+
author: ama
6+
permalink: /ama/how-to-use-input-dialog-in-intellij-plugin
7+
published: true
8+
categories: [tutorial]
9+
tags: [intellij, productivity, java, codever, plugins]
10+
---
11+
12+
## Use case
13+
I thought it would be practical to be able to search for search snippets directly from IntelliJ, without having to go to
14+
the browser, something like in the following demo:
15+
16+
{% include image.html url="https://raw.githubusercontent.com/codeverland/codever-intellij-plugin/master/documentation/img/plugin-showcase-search-800x456.gif" description="IntelliJ Plugin - Search snippet from IDE demo" %}
17+
18+
The implementation is fairly easy, so let's see how it's done.
19+
20+
<!--more-->
21+
22+
## Implementation
23+
24+
All the action happens in the `SearchonCodeverDialog` class which extends `AnAction`[^1] and overrides its `actionPerformed`
25+
method:
26+
27+
```java
28+
public class SearchOnCodeverDialog extends AnAction {
29+
30+
@Override
31+
public void actionPerformed(@NotNull AnActionEvent event) {
32+
Project project = event.getData(PlatformDataKeys.PROJECT);
33+
34+
final Editor editor = event.getRequiredData(CommonDataKeys.EDITOR);
35+
final String selectedText = editor.getSelectionModel().getSelectedText();
36+
37+
String queryTxt;
38+
if (selectedText != null) {
39+
queryTxt = Messages.showInputDialog("Input query to search in My Snippets", "Codever Search", Messages.getQuestionIcon(), selectedText, null);
40+
} else {
41+
queryTxt = Messages.showInputDialog(project, "Input query to search in My Snippets", "Codever Search", Messages.getQuestionIcon());
42+
}
43+
String url = "https://www.codever.land/search?sd=my-snippets&q=" + queryTxt;
44+
if (queryTxt != null) {
45+
BrowserUtil.browse(url);
46+
}
47+
48+
}
49+
}
50+
```
51+
52+
[^1]: <https://plugins.jetbrains.com/docs/intellij/basic-action-system.html>
53+
54+
The user is asked for input via the method `Messages.showInputDialog` where any `selectedText`, if existing, is passed.
55+
The result, `queryTxt`, contains the user input.
56+
57+
Of course, you need to define this action in `plugin.xml` file, which I did both for the Editor Menu, and the Console Menu:
58+
59+
```xml
60+
<actions>
61+
.....
62+
<action
63+
id="Codever.Search.Editor"
64+
class="codever.intellij.plugin.SearchOnCodeverDialog"
65+
text="Search on Codever"
66+
description="Launches dialog to input query search on Codever"
67+
icon="SaveToCodeverPluginIcons.CODEVER_ACTION_ICON">
68+
<add-to-group group-id="EditorPopupMenu" anchor="last"/>
69+
</action>
70+
<action
71+
id="Codever.Search.Console"
72+
class="codever.intellij.plugin.SearchOnCodeverDialog"
73+
text="Search on Codever"
74+
description="Launches dialog to input query search on Codever"
75+
icon="SaveToCodeverPluginIcons.CODEVER_ACTION_ICON">
76+
<add-to-group group-id="ConsoleEditorPopupMenu" anchor="last"/>
77+
</action>
78+
</actions>
79+
```
80+
81+
So simple and straight forward. If you want to learn also how to get the selected text, get file info and debug an IntelliJ
82+
see my previous post related to IntelliJ Plugins - [My first IntelliJ plugin to help me save code snippets to Codever]({% post_url 2020-06-07-first-intellij-plugin-to-save-snippets-to-codever %})
83+
84+
> The plugin is available to download and install in
85+
> [JetBrain Plugins Repository](https://plugins.jetbrains.com/plugin/14456-codever-snippets/)
86+
> and the source code is available on [Github](https://github.com/codeverland/codever-intellij-plugin)

0 commit comments

Comments
 (0)