Source code: com/maddyhome/idea/vim/helper/EditorData.java
1 package com.maddyhome.idea.vim.helper;
2
3 /*
4 * IdeaVim - A Vim emulator plugin for IntelliJ Idea
5 * Copyright (C) 2003 Rick Maddy
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21
22 import com.intellij.openapi.editor.Editor;
23 import com.intellij.openapi.fileEditor.FileEditor;
24 import com.intellij.openapi.fileEditor.FileEditorManager;
25 import com.intellij.openapi.fileEditor.TextEditor;
26 import com.intellij.openapi.project.Project;
27 import com.intellij.openapi.project.ProjectManager;
28 import com.intellij.openapi.vfs.VirtualFile;
29 import com.maddyhome.idea.vim.command.VisualChange;
30 import com.maddyhome.idea.vim.command.VisualRange;
31 import com.maddyhome.idea.vim.group.MarkGroup;
32 import com.maddyhome.idea.vim.undo.UndoManager;
33
34 /**
35 * This class is used to manipulate editor specific data. Each editor has a user defined map associated with it.
36 * These methods provide convenient methods for working with that Vim Plugin specific data.
37 */
38 public class EditorData
39 {
40 /**
41 * This is used to initialize each new editor that gets created.
42 * @param editor The editor to initialize
43 */
44 public static void initializeEditor(Editor editor)
45 {
46 // Add a document listener so we can update the position of the editor specific marks each time the
47 // contents change
48 if (!editor.isViewer())
49 {
50 editor.getDocument().addDocumentListener(new MarkGroup.MarkUpdater(editor));
51 }
52 UndoManager.getInstance().editorOpened(editor);
53 }
54
55 /**
56 * This is used to clean up editors whenever they are closed.
57 * @param editor The editor to cleanup
58 */
59 public static void uninitializeEditor(Editor editor)
60 {
61 UndoManager.getInstance().editorClosed(editor);
62 }
63
64 /**
65 * This gets the last column the cursor was in for the editor.
66 * @param editor The editr to get the last column from
67 * @return Returns the last column as set by {@link #setLastColumn} or the current cursor column
68 */
69 public static int getLastColumn(Editor editor)
70 {
71 Integer col = (Integer)editor.getUserData(LAST_COLUMN);
72 if (col == null)
73 {
74 return EditorHelper.getCurrentVisualColumn(editor);
75 }
76 else
77 {
78 return col.intValue();
79 }
80 }
81
82 /**
83 * Sets the last column for this editor
84 * @param col The column
85 * @param editor The editor
86 */
87 public static void setLastColumn(Editor editor, int col)
88 {
89 editor.putUserData(LAST_COLUMN, new Integer(col));
90 }
91
92 /**
93 * Gets the previous visual range for the editor.
94 * @param editor The editor to get the range for
95 * @return The last visual range, null if no previous range
96 */
97 public static VisualRange getLastVisualRange(Editor editor)
98 {
99 VisualRange res = (VisualRange)editor.getUserData(VISUAL);
100 return res;
101 }
102
103 /**
104 * Sets the previous visual range for the editor.
105 * @param editor The editor to set the range for
106 * @param range The visual range
107 */
108 public static void setLastVisualRange(Editor editor, VisualRange range)
109 {
110 editor.putUserData(VISUAL, range);
111 }
112
113 /**
114 * Gets the previous visual operator range for the editor.
115 * @param editor The editor to get the range for
116 * @return The last visual range, null if no previous range
117 */
118 public static VisualChange getLastVisualOperatorRange(Editor editor)
119 {
120 VisualChange res = (VisualChange)editor.getUserData(VISUAL_OP);
121 return res;
122 }
123
124 /**
125 * Sets the previous visual operator range for the editor.
126 * @param editor The editor to set the range for
127 * @param range The visual range
128 */
129 public static void setLastVisualOperatorRange(Editor editor, VisualChange range)
130 {
131 editor.putUserData(VISUAL_OP, range);
132 }
133
134 /**
135 * Gets the project associated with the editor.
136 * @param editor The editor to get the project for
137 * @return The editor's project
138 */
139 public static Project getProject(Editor editor)
140 {
141 Project proj = (Project)editor.getUserData(PROJECT);
142 if (proj == null)
143 {
144 // If we don't have the project already we need to scan all open projects and check all their
145 // open editors until there is a match
146 Project[] projs = ProjectManager.getInstance().getOpenProjects();
147 for (int p = 0; p < projs.length; p++)
148 {
149 FileEditorManager fMgr = FileEditorManager.getInstance(projs[p]);
150 VirtualFile[] files = fMgr.getOpenFiles();
151 for (int i = 0; i < files.length; i++)
152 {
153 FileEditor[] editors = fMgr.getEditors(files[i]);
154 for (int e = 0; e < editors.length; e++)
155 {
156 if (editors[0] instanceof TextEditor && ((TextEditor)editors[0]).getEditor().equals(editor))
157 {
158 proj = projs[p];
159 editor.putUserData(PROJECT, proj);
160 break;
161 }
162 }
163 }
164 }
165 }
166
167 return proj;
168 }
169
170 /**
171 * Gets the virtual file associated with this editor
172 * @param editor The editor
173 * @return The virtual file for the editor
174 */
175 public static VirtualFile getVirtualFile(Editor editor)
176 {
177 VirtualFile file = (VirtualFile)editor.getUserData(FILE);
178 if (file == null)
179 {
180 Project[] projs = ProjectManager.getInstance().getOpenProjects();
181 for (int p = 0; p < projs.length; p++)
182 {
183 FileEditorManager fMgr = FileEditorManager.getInstance(projs[p]);
184 VirtualFile[] files = fMgr.getOpenFiles();
185 for (int i = 0; i < files.length; i++)
186 {
187 FileEditor[] editors = fMgr.getEditors(files[i]);
188 for (int e = 0; e < editors.length; e++)
189 {
190 if (editors[0] instanceof TextEditor && ((TextEditor)editors[0]).getEditor().equals(editor))
191 {
192 file = files[i];
193 editor.putUserData(FILE, file);
194 break;
195 }
196 }
197 }
198 }
199 }
200
201 return file;
202 }
203
204 /**
205 * This is a static helper - no instances needed
206 */
207 private EditorData() {}
208
209 private static final String LAST_COLUMN = "lastColumn";
210 private static final String PROJECT = "project";
211 private static final String FILE = "virtualFile";
212 private static final String VISUAL = "lastVisual";
213 private static final String VISUAL_OP = "lastVisualOp";
214 }