add stackoverflow-plugin-gradle project

This commit is contained in:
Waldemar Penner 2020-06-07 09:55:29 +02:00
parent 357ecb0341
commit bad42826d5
5 changed files with 122 additions and 0 deletions

View File

@ -0,0 +1,25 @@
plugins {
id 'java'
id 'org.jetbrains.intellij' version '0.4.21'
}
group 'com.baeldung'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
// See https://github.com/JetBrains/gradle-intellij-plugin/
intellij {
version '2020.1.1'
}
patchPluginXml {
changeNotes """
Add change notes here.<br>
<em>most HTML tags may be used</em>"""
}

View File

@ -0,0 +1,3 @@
rootProject.name = 'stackoverflow-plugin-gradle'
include 'java'

View File

@ -0,0 +1,12 @@
package com.baeldung.intellij.stackoverflowplugin;
import com.intellij.ide.BrowserUtil;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
public class AskQuestionAction extends AnAction {
@Override
public void actionPerformed(AnActionEvent e) {
BrowserUtil.browse("https://stackoverflow.com/questions/ask");
}
}

View File

@ -0,0 +1,41 @@
package com.baeldung.intellij.stackoverflowplugin;
import com.intellij.ide.BrowserUtil;
import com.intellij.lang.Language;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.editor.CaretModel;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;
import java.util.Optional;
public class SearchAction extends AnAction {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
Optional<PsiFile> psiFile = Optional.ofNullable(e.getData(LangDataKeys.PSI_FILE));
String languageTag = psiFile
.map(PsiFile::getLanguage)
.map(Language::getDisplayName)
.map(String::toLowerCase)
.map(lang -> "[" + lang + "]")
.orElse("");
Editor editor = e.getRequiredData(CommonDataKeys.EDITOR);
CaretModel caretModel = editor.getCaretModel();
String selectedText = caretModel.getCurrentCaret().getSelectedText();
BrowserUtil.browse("https://stackoverflow.com/search?q=" + languageTag + selectedText);
}
@Override
public void update(AnActionEvent e) {
Editor editor = e.getRequiredData(CommonDataKeys.EDITOR);
CaretModel caretModel = editor.getCaretModel();
e.getPresentation().setEnabledAndVisible(caretModel.getCurrentCaret().hasSelection());
}
}

View File

@ -0,0 +1,41 @@
<idea-plugin>
<id>com.baeldung.stackoverflow-plugin-gradle</id>
<name>StackOverflow</name>
<vendor email="waldemar.penner91@gmail.com" url="https://baeldung.com">Baeldung</vendor>
<description><![CDATA[
Ask Question or search on Stack Overflow
]]></description>
<!-- please see https://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html
on how to target different products -->
<depends>com.intellij.modules.platform</depends>
<extensions defaultExtensionNs="com.intellij">
<!-- Add your extensions here -->
</extensions>
<actions>
<action
id="StackOverflow.AskQuestion.ToolsMenu"
class="com.baeldung.intellij.stackoverflowplugin.AskQuestionAction"
text="Ask Question on Stack Overflow"
description="Ask a Question on Stack Overflow">
<add-to-group group-id="ToolsMenu" anchor="last"/>
</action>
<action
id="StackOverflow.Search.Editor"
class="com.baeldung.intellij.stackoverflowplugin.SearchAction"
text="Search on Stack Overflow"
description="Search on Stack Overflow">
<add-to-group group-id="EditorPopupMenu" anchor="last"/>
</action>
<action
id="StackOverflow.Search.Console"
class="com.baeldung.intellij.stackoverflowplugin.SearchAction"
text="Search on Stack Overflow"
description="Search on Stack Overflow">
<add-to-group group-id="ConsoleEditorPopupMenu" anchor="last"/>
</action>
</actions>
</idea-plugin>