44import com .intellij .openapi .actionSystem .ActionUpdateThread ;
55import com .intellij .openapi .actionSystem .AnActionEvent ;
66import com .intellij .openapi .actionSystem .CommonDataKeys ;
7+ import com .intellij .openapi .diagnostic .Logger ;
8+ import com .intellij .openapi .editor .Document ;
79import com .intellij .openapi .editor .Editor ;
810import com .intellij .openapi .editor .SelectionModel ;
911import com .intellij .openapi .fileTypes .FileType ;
1214import com .intellij .openapi .project .Project ;
1315import com .intellij .openapi .util .Key ;
1416import com .intellij .openapi .vfs .VirtualFile ;
15- import com .intellij .testFramework .LightVirtualFile ;
1617import org .jetbrains .annotations .NotNull ;
1718import org .jetbrains .annotations .Nullable ;
1819
1920import static com .devoxx .genie .ui .util .WindowPluginUtil .ensureToolWindowVisible ;
2021
2122public class AddSnippetAction extends DumbAwareAction {
2223
24+ private static final Logger LOG = Logger .getInstance (AddSnippetAction .class );
25+
2326 public static final String CODE_SNIPPET = "codeSnippet" ;
2427 public static final Key <VirtualFile > ORIGINAL_FILE_KEY = Key .create ("ORIGINAL_FILE" );
2528 public static final Key <String > SELECTED_TEXT_KEY = Key .create ("SELECTED_TEXT" );
2629 public static final Key <Integer > SELECTION_START_KEY = Key .create ("SELECTION_START" );
2730 public static final Key <Integer > SELECTION_END_KEY = Key .create ("SELECTION_END" );
31+ public static final Key <Integer > SELECTION_START_LINE_KEY = Key .create ("SELECTION_START_LINE" );
32+ public static final Key <Integer > SELECTION_END_LINE_KEY = Key .create ("SELECTION_END_LINE" );
2833
2934 // We use an unknown file type to represent code snippets
3035 private final FileType fileType =
@@ -47,7 +52,7 @@ public void actionPerformed(@NotNull AnActionEvent e) {
4752 SelectionModel selectionModel = editor .getSelectionModel ();
4853 String selectedText = selectionModel .getSelectedText ();
4954 if (selectedText != null ) {
50- createAndAddVirtualFile (e .getProject (), selectedFile , selectionModel , selectedText );
55+ createAndAddVirtualFile (e .getProject (), selectedFile , editor , selectedText );
5156 } else {
5257 // No text selected, add complete file
5358 addSelectedFile (e .getProject (), selectedFile );
@@ -71,23 +76,35 @@ private static void addSelectedFile(Project project, VirtualFile selectedFile) {
7176 /**
7277 * Create a virtual file and add it to the file list manager.
7378 *
74- * @param project
79+ * @param project the project
7580 * @param originalFile the original file
76- * @param selectionModel the selection model
81+ * @param editor the editor
7782 * @param selectedText the selected text
7883 */
79- private void createAndAddVirtualFile (@ Nullable Project project , @ NotNull VirtualFile originalFile ,
80- @ NotNull SelectionModel selectionModel ,
84+ private void createAndAddVirtualFile (@ Nullable Project project ,
85+ @ NotNull VirtualFile originalFile ,
86+ @ NotNull Editor editor ,
8187 String selectedText ) {
82- LightVirtualFile virtualFile = new LightVirtualFile (originalFile .getName (), selectedText );
83- virtualFile .setFileType (fileType );
84- virtualFile .putUserData (ORIGINAL_FILE_KEY , originalFile );
85- virtualFile .putUserData (SELECTED_TEXT_KEY , selectedText );
86- virtualFile .putUserData (SELECTION_START_KEY , selectionModel .getSelectionStart ());
87- virtualFile .putUserData (SELECTION_END_KEY , selectionModel .getSelectionEnd ());
88- FileListManager .getInstance ().addFile (project , virtualFile );
89- }
88+ if (project == null ) {
89+ LOG .error ("Project is null" );
90+ return ;
91+ }
92+ SelectionModel selectionModel = editor .getSelectionModel ();
93+ Document document = editor .getDocument ();
94+
95+ int startOffset = selectionModel .getSelectionStart ();
96+ int endOffset = selectionModel .getSelectionEnd ();
97+ int startLine = document .getLineNumber (startOffset );
98+ int endLine = document .getLineNumber (endOffset );
99+ originalFile .putUserData (ORIGINAL_FILE_KEY , originalFile );
100+ originalFile .putUserData (SELECTED_TEXT_KEY , selectedText );
101+ originalFile .putUserData (SELECTION_START_KEY , startOffset );
102+ originalFile .putUserData (SELECTION_END_KEY , endOffset );
103+ originalFile .putUserData (SELECTION_START_LINE_KEY , startLine );
104+ originalFile .putUserData (SELECTION_END_LINE_KEY , endLine );
90105
106+ FileListManager .getInstance ().addFile (project , originalFile );
107+ }
91108
92109 @ Override
93110 public @ NotNull ActionUpdateThread getActionUpdateThread () {
0 commit comments