├── .gitignore ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src └── main ├── java ├── CustomTextEditor.java ├── GutterTextAnnotation.java ├── MouseEventConsumer.java ├── SelectionHandler.java ├── SnapToolWindow.java └── SnapView.java └── resources ├── META-INF ├── plugin.xml └── pluginIcon.svg └── icons ├── action.svg ├── action_dark.svg ├── camera_final.svg └── camera_initial.svg /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle/ 2 | .idea/ 3 | build/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CodeSnapshot 2 | 3 | This plugin allows you to save your code as an image. By simply selecting your code, you can save you code with proper syntax highlighting. 4 | The image adapts to the current setting of your intellij editor like background color. 5 | 6 | ![Demo](https://user-images.githubusercontent.com/12782512/88483811-ed7abd00-cf87-11ea-9143-704b786e8134.gif) 7 | 8 | 9 | ## How to use 10 | 1. Select some code 11 | 2. Right click and select `Code Snapshot` 12 | 3. Use keyboard or mouse to change your selection if you want 13 | 4. Click on camera to save 14 | 5. Select image name, location and done ! 15 | 16 | ## Link 17 | [Code Snapshot](https://plugins.jetbrains.com/plugin/14775-codesnapshot) 18 | 19 | ## Credits 20 | 21 | Thank you [photo3idea-studio](https://www.flaticon.com/authors/photo3idea-studio), [freepik](https://www.flaticon.com/authors/freepik) for the icons 22 | 23 | ## Demo 24 | 25 | 1. [_**MONOKAI PRO**_](https://plugins.jetbrains.com/plugin/12571-monokai-materialized-color-scheme) 26 | 27 | ![monokai](https://user-images.githubusercontent.com/12782512/93019402-64314f80-f5f4-11ea-80aa-61004d8fe363.png) 28 | 29 | 2. [**_DRACULA_**](https://github.com/dracula/jetbrains) 30 | 31 | ![dracula](https://user-images.githubusercontent.com/12782512/93019343-fb49d780-f5f3-11ea-9ba1-c4579a8afa89.png) 32 | 33 | 3. [_**SOLARIZED_DARK**_](https://plugins.jetbrains.com/plugin/12112-solarized-theme) 34 | 35 | ![solarized_dark](https://user-images.githubusercontent.com/12782512/93019496-f6d1ee80-f5f4-11ea-9ffa-b00dc1764094.png) 36 | 37 | 4. [**_GRUVBOX_DARK_**](https://plugins.jetbrains.com/plugin/12310-gruvbox-theme) 38 | 39 | ![gruvbox_dark](https://user-images.githubusercontent.com/12782512/93019533-37316c80-f5f5-11ea-9417-2fd0f1a0f241.png) 40 | 41 | 5. [**_DEFAULT_**](https://plugins.jetbrains.com/plugin/12697-intellij-light-theme) 42 | 43 | ![default](https://user-images.githubusercontent.com/12782512/93019573-824b7f80-f5f5-11ea-9389-1352ee386024.png) 44 | 45 | 46 | ## LICENSE 47 | 48 | ``` 49 | Licensed under the Apache License, Version 2.0 (the "License"); 50 | you may not use this file except in compliance with the License. 51 | You may obtain a copy of the License at 52 | 53 | http://www.apache.org/licenses/LICENSE-2.0 54 | 55 | Unless required by applicable law or agreed to in writing, software 56 | distributed under the License is distributed on an "AS IS" BASIS, 57 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 58 | See the License for the specific language governing permissions and 59 | limitations under the License. 60 | ``` 61 | 62 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | id 'org.jetbrains.intellij' version '0.4.21' 4 | } 5 | 6 | group 'com.mohak1712' 7 | 8 | repositories { 9 | mavenCentral() 10 | } 11 | 12 | sourceCompatibility = 1.8 13 | 14 | dependencies { 15 | testCompile group: 'junit', name: 'junit', version: '4.12' 16 | implementation 'org.jetbrains:annotations:15.0' 17 | } 18 | 19 | patchPluginXml { 20 | sinceBuild '193' 21 | untilBuild '202.*' 22 | } 23 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mohak1712/CodeSnapshot/4522919252cbc0bf619d145687bd3a3e4860654b/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.9-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'code-snapshot' 2 | 3 | -------------------------------------------------------------------------------- /src/main/java/CustomTextEditor.java: -------------------------------------------------------------------------------- 1 | import com.intellij.codeInsight.daemon.impl.HighlightInfo; 2 | import com.intellij.lang.annotation.HighlightSeverity; 3 | import com.intellij.openapi.editor.EditorSettings; 4 | import com.intellij.openapi.editor.colors.EditorColorsManager; 5 | import com.intellij.openapi.editor.colors.EditorColorsScheme; 6 | import com.intellij.openapi.editor.ex.EditorEx; 7 | import com.intellij.openapi.editor.impl.EditorImpl; 8 | import com.intellij.openapi.editor.markup.HighlighterLayer; 9 | import com.intellij.openapi.fileTypes.FileType; 10 | import com.intellij.openapi.project.Project; 11 | import com.intellij.ui.EditorTextField; 12 | 13 | import javax.swing.*; 14 | import java.awt.*; 15 | 16 | public class CustomTextEditor extends EditorTextField { 17 | 18 | public CustomTextEditor(String text, Project project, FileType fileType) { 19 | super(text, project, fileType); 20 | } 21 | 22 | @Override 23 | protected EditorEx createEditor() { 24 | EditorEx editor = super.createEditor(); 25 | setupEditor(editor); 26 | setupScroller(editor); 27 | setupListeners(editor); 28 | setupSettings(editor); 29 | setupHighlightingFilter((EditorImpl) editor); 30 | return editor; 31 | } 32 | 33 | private void setupEditor(EditorEx editor) { 34 | editor.setOneLineMode(false); 35 | editor.setInsertMode(false); 36 | editor.setCaretEnabled(false); 37 | editor.getGutter().registerTextAnnotation(new GutterTextAnnotation(this)); 38 | editor.setViewer(true); 39 | editor.setVerticalScrollbarVisible(true); 40 | EditorColorsManager colorsManager = EditorColorsManager.getInstance(); 41 | EditorColorsScheme customGlobalScheme = colorsManager.getSchemeForCurrentUITheme(); 42 | editor.setColorsScheme(customGlobalScheme); 43 | editor.getColorsScheme().setFontPreferences(customGlobalScheme.getFontPreferences()); 44 | } 45 | 46 | private void setupScroller(EditorEx editor) { 47 | JScrollPane scrollPane = editor.getScrollPane(); 48 | scrollPane.getVerticalScrollBar().setPreferredSize(new Dimension(0, 0)); 49 | } 50 | 51 | private void setupSettings(EditorEx editor) { 52 | EditorSettings settings = editor.getSettings(); 53 | settings.setUseSoftWraps(true); 54 | settings.setAllowSingleLogicalLineFolding(true); 55 | settings.setAutoCodeFoldingEnabled(false); 56 | settings.setFoldingOutlineShown(false); 57 | editor.reinitSettings(); 58 | } 59 | 60 | private void setupListeners(EditorEx editor) { 61 | editor.addEditorMouseMotionListener(new MouseEventConsumer()); 62 | editor.addEditorMouseListener(new MouseEventConsumer()); 63 | } 64 | 65 | private void setupHighlightingFilter(EditorImpl editor) { 66 | editor.setHighlightingFilter(rangeHighlighter -> { 67 | if (rangeHighlighter.getLayer() == HighlighterLayer.ERROR) 68 | return false; 69 | HighlightInfo info = HighlightInfo.fromRangeHighlighter(rangeHighlighter); 70 | if (info != null && info.getSeverity().compareTo(HighlightSeverity.GENERIC_SERVER_ERROR_OR_WARNING) >= 0) 71 | return false; 72 | return true; 73 | }); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/main/java/GutterTextAnnotation.java: -------------------------------------------------------------------------------- 1 | import com.intellij.openapi.actionSystem.AnAction; 2 | import com.intellij.openapi.editor.Editor; 3 | import com.intellij.openapi.editor.TextAnnotationGutterProvider; 4 | import com.intellij.openapi.editor.colors.ColorKey; 5 | import com.intellij.openapi.editor.colors.EditorFontType; 6 | import org.jetbrains.annotations.NotNull; 7 | import org.jetbrains.annotations.Nullable; 8 | 9 | import java.awt.*; 10 | import java.util.List; 11 | 12 | /** 13 | * A gutter with spaces as text to provide left margin to Editor. 14 | * There has to be a better way of doing this. 15 | */ 16 | public class GutterTextAnnotation implements TextAnnotationGutterProvider { 17 | 18 | private CustomTextEditor customTextEditor; 19 | 20 | public GutterTextAnnotation(CustomTextEditor customTextEditor) { 21 | this.customTextEditor = customTextEditor; 22 | } 23 | 24 | @NotNull 25 | @Override 26 | public String getLineText(int line, Editor editor) { 27 | return " "; 28 | } 29 | 30 | @Override 31 | public @Nullable ColorKey getColor(int line, Editor editor) { 32 | return null; 33 | } 34 | 35 | @Override 36 | public @Nullable String getToolTip(int line, Editor editor) { 37 | return null; 38 | } 39 | 40 | @Override 41 | public EditorFontType getStyle(int line, Editor editor) { 42 | return null; 43 | } 44 | 45 | @Override 46 | public @Nullable Color getBgColor(int line, Editor editor) { 47 | return customTextEditor.getBackground(); 48 | } 49 | 50 | @Override 51 | public List getPopupActions(int line, Editor editor) { 52 | return null; 53 | } 54 | 55 | @Override 56 | public void gutterClosed() { 57 | 58 | } 59 | 60 | @Override 61 | public boolean useMargin() { 62 | return false; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/MouseEventConsumer.java: -------------------------------------------------------------------------------- 1 | import com.intellij.openapi.editor.event.EditorMouseEvent; 2 | import com.intellij.openapi.editor.event.EditorMouseListener; 3 | import com.intellij.openapi.editor.event.EditorMouseMotionListener; 4 | import org.jetbrains.annotations.NotNull; 5 | 6 | public class MouseEventConsumer implements EditorMouseListener, EditorMouseMotionListener { 7 | 8 | @Override 9 | public void mousePressed(@NotNull EditorMouseEvent event) { 10 | event.consume(); 11 | } 12 | 13 | @Override 14 | public void mouseClicked(@NotNull EditorMouseEvent event) { 15 | event.consume(); 16 | } 17 | 18 | @Override 19 | public void mouseReleased(@NotNull EditorMouseEvent event) { 20 | event.consume(); 21 | } 22 | 23 | @Override 24 | public void mouseEntered(@NotNull EditorMouseEvent event) { 25 | event.consume(); 26 | } 27 | 28 | @Override 29 | public void mouseExited(@NotNull EditorMouseEvent event) { 30 | event.consume(); 31 | } 32 | 33 | @Override 34 | public void mouseMoved(@NotNull EditorMouseEvent event) { 35 | event.consume(); 36 | } 37 | 38 | @Override 39 | public void mouseDragged(@NotNull EditorMouseEvent event) { 40 | event.consume(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/SelectionHandler.java: -------------------------------------------------------------------------------- 1 | import com.intellij.openapi.actionSystem.AnAction; 2 | import com.intellij.openapi.actionSystem.AnActionEvent; 3 | import com.intellij.openapi.actionSystem.LangDataKeys; 4 | import com.intellij.openapi.actionSystem.PlatformDataKeys; 5 | import com.intellij.openapi.editor.Editor; 6 | import com.intellij.openapi.editor.event.SelectionEvent; 7 | import com.intellij.openapi.editor.event.SelectionListener; 8 | import com.intellij.openapi.editor.impl.EditorImpl; 9 | import com.intellij.openapi.wm.ToolWindow; 10 | import com.intellij.openapi.wm.ToolWindowManager; 11 | import com.intellij.psi.PsiDocumentManager; 12 | import com.intellij.psi.PsiFile; 13 | import com.intellij.psi.PsiFileFactory; 14 | import com.intellij.ui.EditorTextField; 15 | import com.intellij.ui.components.JBPanel; 16 | import com.intellij.ui.components.JBScrollPane; 17 | import com.intellij.ui.content.Content; 18 | import org.jetbrains.annotations.NotNull; 19 | 20 | public class SelectionHandler extends AnAction { 21 | 22 | @Override 23 | public void actionPerformed(@NotNull AnActionEvent e) { 24 | Editor editor = e.getData(PlatformDataKeys.EDITOR); 25 | PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE); 26 | String selectedText = editor.getSelectionModel().getSelectedText(); 27 | editor.getSelectionModel().addSelectionListener(new SelectionListener() { 28 | @Override 29 | public void selectionChanged(@NotNull SelectionEvent e) { 30 | updateViewOnSelectionChanged(e); 31 | } 32 | }); 33 | 34 | ToolWindow toolWindow = ToolWindowManager.getInstance(e.getProject()).getToolWindow("CodeSnapshot"); 35 | if (!toolWindow.isVisible()) { 36 | toolWindow.show(() -> { 37 | }); 38 | } 39 | 40 | JBPanel panel = getPanel(toolWindow); 41 | updateUi(panel); 42 | updateText(psiFile, selectedText, (EditorTextField) panel.getComponent(1)); 43 | } 44 | 45 | private void updateViewOnSelectionChanged(@NotNull SelectionEvent e) { 46 | Editor editor = e.getEditor(); 47 | PsiFile psiFile = PsiDocumentManager.getInstance(editor.getProject()).getPsiFile(editor.getDocument()); 48 | String selectedText = editor.getSelectionModel().getSelectedText(); 49 | 50 | if (selectedText == null || selectedText.isEmpty()) { 51 | return; 52 | } 53 | 54 | ToolWindow toolWindow = ToolWindowManager.getInstance(editor.getProject()).getToolWindow("CodeSnapshot"); 55 | if (!toolWindow.isVisible()) { 56 | return; 57 | } 58 | 59 | JBPanel panel = getPanel(toolWindow); 60 | updateUi(panel); 61 | updateText(psiFile, selectedText, (EditorTextField) panel.getComponent(1)); 62 | } 63 | 64 | private void updateUi(JBPanel panel) { 65 | panel.getComponent(0).setVisible(false); 66 | panel.getComponent(1).setVisible(true); 67 | } 68 | 69 | private void updateText(PsiFile psiFile, String selectedText, EditorTextField codeView) { 70 | PsiFile selectedTextAsPsi = PsiFileFactory.getInstance(codeView.getProject()) 71 | .createFileFromText("", psiFile.getLanguage(), selectedText); 72 | codeView.setDocument(selectedTextAsPsi.getViewProvider().getDocument()); 73 | } 74 | 75 | @Override 76 | public void update(@NotNull AnActionEvent e) { 77 | Editor edit = e.getData(PlatformDataKeys.EDITOR); 78 | e.getPresentation().setEnabledAndVisible(shouldEnable(edit)); 79 | } 80 | 81 | private boolean shouldEnable(Editor edit) { 82 | return edit != null && edit.getSelectionModel().getSelectedText() != null 83 | && edit.getSelectionModel().getSelectedText().length() > 0; 84 | } 85 | 86 | private JBPanel getPanel(ToolWindow toolWindow) { 87 | Content content = toolWindow.getContentManager().getContent(0); 88 | JBScrollPane scrollPane = (JBScrollPane) content.getComponent(); 89 | JBPanel dataPanel = (JBPanel) scrollPane.getViewport().getView(); 90 | return dataPanel; 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/main/java/SnapToolWindow.java: -------------------------------------------------------------------------------- 1 | import com.intellij.openapi.project.Project; 2 | import com.intellij.openapi.wm.ToolWindow; 3 | import com.intellij.openapi.wm.ToolWindowFactory; 4 | import com.intellij.ui.components.JBPanel; 5 | import com.intellij.ui.components.JBScrollPane; 6 | import com.intellij.ui.content.Content; 7 | import com.intellij.ui.content.ContentFactory; 8 | import org.jetbrains.annotations.NotNull; 9 | 10 | import javax.swing.*; 11 | import javax.swing.text.BadLocationException; 12 | import javax.swing.text.Style; 13 | import javax.swing.text.StyleConstants; 14 | import javax.swing.text.StyledDocument; 15 | import java.awt.*; 16 | import java.util.ArrayList; 17 | import java.util.List; 18 | 19 | public class SnapToolWindow implements ToolWindowFactory { 20 | 21 | @Override 22 | public void createToolWindowContent(@NotNull Project project, com.intellij.openapi.wm.@NotNull ToolWindow toolWindow) { 23 | SnapView snapView = new SnapView(project); 24 | ContentFactory instance = ContentFactory.SERVICE.getInstance(); 25 | Content content = instance.createContent(snapView.getComponent(), "", false); 26 | toolWindow.getContentManager().addContent(content); 27 | } 28 | 29 | @Override 30 | public void init(com.intellij.openapi.wm.ToolWindow window) { 31 | JBPanel panel = getPanel(window); 32 | panel.getComponent(1).setVisible(false); 33 | JTextPane instructions = (JTextPane) panel.getComponent(0); 34 | formatText(instructions); 35 | } 36 | 37 | private void formatText(JTextPane textPane) { 38 | textPane.setBackground(new Color(60, 63, 65)); 39 | textPane.setFont(new Font("Monospaced", Font.PLAIN, 14)); 40 | textPane.setEditable(false); 41 | StyledDocument doc = textPane.getStyledDocument(); 42 | 43 | Style keywordStyle = textPane.addStyle("keyword", null); 44 | StyleConstants.setForeground(keywordStyle, new Color(204, 120, 50)); 45 | 46 | Style paramStyle = textPane.addStyle("param", null); 47 | StyleConstants.setForeground(paramStyle, new Color(106, 135, 89)); 48 | getInstructions().forEach(s -> { 49 | try { 50 | doc.insertString(doc.getLength(), s.substring(0, 6), keywordStyle); 51 | doc.insertString(doc.getLength(), s.substring(6, s.lastIndexOf(')')), paramStyle); 52 | doc.insertString(doc.getLength(), ")\n\n", keywordStyle); 53 | } catch (BadLocationException e) { 54 | e.printStackTrace(); 55 | } 56 | }); 57 | 58 | } 59 | 60 | private List getInstructions() { 61 | List instructions = new ArrayList<>(); 62 | instructions.add("print(\"1. Select some text in editor\")"); 63 | instructions.add("print(\"2. Right click and select `Code Snapshot`\")"); 64 | instructions.add("print(\"3. Drag mouse or use keyboard to change selection\")"); 65 | instructions.add("print(\"4. Click on camera to save as image\")"); 66 | return instructions; 67 | } 68 | 69 | private JBPanel getPanel(ToolWindow toolWindow) { 70 | Content content = toolWindow.getContentManager().getContent(0); 71 | JBScrollPane scrollPane = (JBScrollPane) content.getComponent(); 72 | JBPanel dataPanel = (JBPanel) scrollPane.getViewport().getView(); 73 | return dataPanel; 74 | } 75 | 76 | @Override 77 | public boolean shouldBeAvailable(@NotNull Project project) { 78 | return true; 79 | } 80 | 81 | @Override 82 | public boolean isDoNotActivateOnStart() { 83 | return true; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/main/java/SnapView.java: -------------------------------------------------------------------------------- 1 | import com.intellij.openapi.fileChooser.FileChooserFactory; 2 | import com.intellij.openapi.fileChooser.FileSaverDescriptor; 3 | import com.intellij.openapi.fileChooser.FileSaverDialog; 4 | import com.intellij.openapi.project.Project; 5 | import com.intellij.openapi.util.IconLoader; 6 | import com.intellij.openapi.vfs.VirtualFileWrapper; 7 | import com.intellij.ui.components.JBPanel; 8 | import com.intellij.ui.components.JBScrollPane; 9 | import com.intellij.ui.components.panels.VerticalLayout; 10 | import com.intellij.util.ui.JBUI; 11 | import com.intellij.util.ui.UIUtil; 12 | import org.jetbrains.annotations.NotNull; 13 | 14 | import javax.imageio.ImageIO; 15 | import javax.swing.*; 16 | import java.awt.*; 17 | import java.awt.event.MouseAdapter; 18 | import java.awt.event.MouseEvent; 19 | import java.awt.image.BufferedImage; 20 | import java.io.IOException; 21 | 22 | public class SnapView { 23 | private final JBScrollPane scrollPane; 24 | private final CustomTextEditor codeView; 25 | 26 | public SnapView(@NotNull Project project) { 27 | VerticalLayout verticalLayout = new VerticalLayout(20); 28 | codeView = new CustomTextEditor("", project, null); 29 | JBPanel panel = new JBPanel(verticalLayout); 30 | JTextPane instructions = new JTextPane(); 31 | instructions.setMargin(JBUI.insets(20)); 32 | panel.add(instructions); 33 | panel.add(codeView); 34 | panel.add(saveAsLabel()); 35 | panel.withBackground(Color.LIGHT_GRAY); 36 | scrollPane = new JBScrollPane(panel); 37 | scrollPane.getVerticalScrollBar().setPreferredSize(new Dimension(0, 0)); 38 | scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); 39 | scrollPane.setBorder(JBUI.Borders.customLine(Color.LIGHT_GRAY, 20, 30, 20, 30)); 40 | } 41 | 42 | private Component saveAsLabel() { 43 | Icon cameraInitialState = IconLoader.getIcon("icons/camera_initial.svg"); 44 | Icon cameraFinalState = IconLoader.getIcon("icons/camera_final.svg"); 45 | JLabel saveAsLabel = new JLabel(cameraInitialState); 46 | saveAsLabel.addMouseListener(new MouseAdapter() { 47 | @Override 48 | public void mouseEntered(MouseEvent e) { 49 | super.mouseEntered(e); 50 | saveAsLabel.setIcon(cameraFinalState); 51 | } 52 | 53 | @Override 54 | public void mouseExited(MouseEvent e) { 55 | super.mouseExited(e); 56 | saveAsLabel.setIcon(cameraInitialState); 57 | } 58 | 59 | @Override 60 | public void mouseClicked(MouseEvent e) { 61 | saveFile(); 62 | } 63 | }); 64 | 65 | return saveAsLabel; 66 | } 67 | 68 | private void saveFile() { 69 | if (!codeView.isVisible()) { 70 | return; 71 | } 72 | 73 | FileSaverDescriptor fileSaverDescriptor = new FileSaverDescriptor("Code Snapshot", "Saves this code as image"); 74 | FileSaverDialog saveFileDialog = FileChooserFactory.getInstance() 75 | .createSaveFileDialog(fileSaverDescriptor, (Project) null); 76 | VirtualFileWrapper fileWrapper = saveFileDialog.save(null, "code.png"); 77 | if (fileWrapper == null) { 78 | //user pressed cancel 79 | return; 80 | } 81 | 82 | BufferedImage image = UIUtil.createImage(codeView, codeView.getWidth(), codeView.getHeight(), BufferedImage.TYPE_INT_ARGB); 83 | codeView.paint(image.getGraphics()); 84 | 85 | try { 86 | ImageIO.write(image, "png", fileWrapper.getFile()); 87 | } catch (IOException e) { 88 | e.printStackTrace(); 89 | } 90 | } 91 | 92 | public JComponent getComponent() { 93 | return scrollPane; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | com.mohak1712.code-snapshot 3 | CodeSnapshot 4 | Mohak Puri 5 | 1.0.2 6 | 7 | 8 | 12 | 13 | 14 | 15 | 18 | 19 | 20 | com.intellij.modules.platform 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/pluginIcon.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | background 5 | 6 | 7 | 8 | Layer 1 9 | 10 | 12 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/main/resources/icons/action.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 | 20 | 22 | 24 | 26 | 28 | 30 | 32 | 33 | -------------------------------------------------------------------------------- /src/main/resources/icons/action_dark.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 | 20 | 22 | 24 | 26 | 28 | 30 | 32 | 33 | -------------------------------------------------------------------------------- /src/main/resources/icons/camera_final.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/main/resources/icons/camera_initial.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | --------------------------------------------------------------------------------