-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
222 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.jar | ||
/.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="PLUGIN_MODULE" version="4"> | ||
<component name="DevKit.ModuleBuildProperties" url="file://$MODULE_DIR$/resources/META-INF/plugin.xml" /> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
<sourceFolder url="file://$MODULE_DIR$/resources" type="java-resource" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
</component> | ||
</module> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# HasteIt | ||
|
||
HasteIt is a plugin for [IDEA](https://www.jetbrains.com/idea/)-based IDEs. | ||
You can quickly share your current file to [hastebin](http://hastebin.com/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<idea-plugin version="2"> | ||
<id>de.lukweb.hasteit</id> | ||
<name>HasteIt</name> | ||
<version>1.0</version> | ||
<vendor email="[email protected]" url="http://www.lukweb.de">LukWeb</vendor> | ||
|
||
<depends>com.intellij.modules.platform</depends> | ||
|
||
<description><![CDATA[ | ||
Enter short description for your plugin here.<br> | ||
<em>most HTML tags may be used</em> | ||
]]></description> | ||
|
||
<change-notes><![CDATA[ | ||
Add change notes here.<br> | ||
<em>most HTML tags may be used</em> | ||
]]> | ||
</change-notes> | ||
|
||
<idea-version since-build="141.0"/> | ||
|
||
<extensions defaultExtensionNs="com.intellij"> | ||
|
||
</extensions> | ||
|
||
<actions> | ||
<action id="de.lukweb.hasteit.MenuHaste" class="de.lukweb.hasteit.MenuHaste" text="Haste it!" | ||
description="Share your selected text to hastebin!" icon="HasteItIcons.UPLOAD_ACTION"> | ||
<add-to-group group-id="ToolsMenu" anchor="last"/> | ||
<add-to-group group-id="EditorPopupMenu" anchor="last"/> | ||
<add-to-group group-id="FileMenu" anchor="last"/> | ||
</action> | ||
</actions> | ||
|
||
</idea-plugin> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package de.lukweb.hasteit; | ||
|
||
import com.intellij.notification.NotificationDisplayType; | ||
import com.intellij.notification.NotificationGroup; | ||
import com.intellij.notification.NotificationType; | ||
import com.intellij.openapi.actionSystem.AnAction; | ||
import com.intellij.openapi.actionSystem.AnActionEvent; | ||
import com.intellij.openapi.actionSystem.CommonDataKeys; | ||
import com.intellij.openapi.actionSystem.PlatformDataKeys; | ||
import com.intellij.openapi.editor.Editor; | ||
import com.intellij.openapi.editor.SelectionModel; | ||
import com.intellij.openapi.progress.ProgressIndicator; | ||
import com.intellij.openapi.progress.ProgressManager; | ||
import com.intellij.openapi.progress.Task; | ||
import com.intellij.psi.PsiFile; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import javax.swing.event.HyperlinkEvent.EventType; | ||
import java.awt.*; | ||
import java.awt.datatransfer.Clipboard; | ||
import java.awt.datatransfer.StringSelection; | ||
import java.io.BufferedReader; | ||
import java.io.DataOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.net.HttpURLConnection; | ||
import java.net.URISyntaxException; | ||
import java.net.URL; | ||
|
||
public class MenuHaste extends AnAction { | ||
|
||
private static final NotificationGroup notificationGroup = new NotificationGroup("HasteIt", NotificationDisplayType.BALLOON, false); | ||
|
||
@Override | ||
public void actionPerformed(AnActionEvent event) { | ||
|
||
PsiFile psiFile = event.getData(CommonDataKeys.PSI_FILE); | ||
|
||
if (psiFile == null) { | ||
err("No file is selected!"); | ||
return; | ||
} | ||
|
||
Editor editor = event.getData(PlatformDataKeys.EDITOR); | ||
|
||
if (editor == null) { | ||
err("There's no editor!"); | ||
return; | ||
} | ||
|
||
SelectionModel selectionModel = editor.getSelectionModel(); | ||
|
||
if (!selectionModel.hasSelection()) { | ||
err("No text is selected!"); | ||
return; | ||
} | ||
|
||
String text = selectionModel.getSelectedText(); | ||
|
||
ProgressManager.getInstance().run(new Task.Backgroundable(event.getProject(), "Uploading to Hastebin") { | ||
@Override | ||
public void run(@NotNull ProgressIndicator progressIndicator) { | ||
progressIndicator.setIndeterminate(true); | ||
String hasteCode = saveTextToHastebin(text); | ||
|
||
if (hasteCode == null) { | ||
progressIndicator.cancel(); | ||
return; | ||
} | ||
|
||
String fullHastebinUrl = "http://hastebin.com/" + hasteCode + "." + psiFile.getFileType().getDefaultExtension(); | ||
|
||
copyToClipboard(fullHastebinUrl); | ||
|
||
notificationGroup.createNotification( | ||
"HasteIt", | ||
"Upload successful! Copied to clipboard! <a href=\"" + fullHastebinUrl + "\">Open in Browser</a> ", | ||
NotificationType.INFORMATION, | ||
(notification, hyperlinkEvent) -> { | ||
if (!hyperlinkEvent.getEventType().equals(EventType.ACTIVATED)) return; | ||
openURL(hyperlinkEvent.getURL()); | ||
} | ||
|
||
).notify(null); | ||
|
||
progressIndicator.cancel(); | ||
} | ||
}); | ||
|
||
} | ||
|
||
private void show(String text) { | ||
show(text, NotificationType.INFORMATION); | ||
} | ||
|
||
private void err(String text) { | ||
show(text, NotificationType.ERROR); | ||
} | ||
|
||
private void show(String text, NotificationType type) { | ||
notificationGroup.createNotification(text, type).notify(null); | ||
} | ||
|
||
private String saveTextToHastebin(String text) { | ||
try { | ||
String url = "http://www.hastebin.com/documents"; | ||
URL obj = new URL(url); | ||
HttpURLConnection con = (HttpURLConnection) obj.openConnection(); | ||
|
||
//add reuqest header | ||
con.setRequestMethod("POST"); | ||
con.setRequestProperty("User-Agent", "Mozilla/5.0"); | ||
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); | ||
|
||
// Send post request | ||
con.setDoOutput(true); | ||
DataOutputStream wr = new DataOutputStream(con.getOutputStream()); | ||
wr.writeBytes(text); | ||
wr.flush(); | ||
wr.close(); | ||
|
||
BufferedReader in = new BufferedReader( | ||
new InputStreamReader(con.getInputStream())); | ||
String inputLine; | ||
StringBuilder response = new StringBuilder(); | ||
|
||
while ((inputLine = in.readLine()) != null) { | ||
response.append(inputLine); | ||
} | ||
in.close(); | ||
|
||
//print result | ||
|
||
String[] split = response.toString().split(":"); | ||
return split[1].replaceAll("}", "").replace("{", "").replaceAll("\"", ""); | ||
|
||
} catch (IOException e) { | ||
err("Error while uploading: " + e.getLocalizedMessage()); | ||
} | ||
return null; | ||
} | ||
|
||
private void copyToClipboard(String text) { | ||
StringSelection selection = new StringSelection(text); | ||
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); | ||
clipboard.setContents(selection, selection); | ||
} | ||
|
||
private void openURL(URL url) { | ||
try { | ||
Desktop.getDesktop().browse(url.toURI()); | ||
} catch (IOException | URISyntaxException e1) { | ||
e1.printStackTrace(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package icons; | ||
|
||
import com.intellij.openapi.util.IconLoader; | ||
|
||
import javax.swing.*; | ||
|
||
public interface HasteItIcons { | ||
|
||
Icon UPLOAD_ACTION = IconLoader.getIcon("hastebin.png"); | ||
|
||
} |