Thread: [Pydev-cvs] org.python.pydev/src/org/python/pydev/editor/actions IOrganizeImports.java, 1.2, 1.3 Py
Brought to you by:
fabioz
From: Fabio Z. <fa...@us...> - 2008-06-02 00:10:24
|
Update of /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/actions In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3412/src/org/python/pydev/editor/actions Modified Files: IOrganizeImports.java PyOrganizeImports.java Log Message: Organize imports for undefined vars. Index: PyOrganizeImports.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/actions/PyOrganizeImports.java,v retrieving revision 1.14 retrieving revision 1.15 diff -C2 -d -r1.14 -r1.15 *** PyOrganizeImports.java 1 Jun 2008 20:44:41 -0000 1.14 --- PyOrganizeImports.java 2 Jun 2008 00:10:31 -0000 1.15 *************** *** 51,55 **** String endLineDelim = ps.getEndLineDelim(); final IDocument doc = ps.getDoc(); ! DocumentRewriteSession session = startWrite(doc); try { --- 51,55 ---- String endLineDelim = ps.getEndLineDelim(); final IDocument doc = ps.getDoc(); ! DocumentRewriteSession session = null; try { *************** *** 57,74 **** //let's see if someone wants to make a better implementation in another plugin... List<IOrganizeImports> participants = ExtensionHelper.getParticipants(ExtensionHelper.PYDEV_ORGANIZE_IMPORTS); ! if (participants.size() == 1) { ! participants.get(0).performArrangeImports(ps, pyEdit); ! } else { ! if (participants.size() > 1) { ! //let's issue a warning... this extension can only have 1 plugin implementing it ! PydevPlugin.log("The organize imports has more than one plugin with this extension point, therefore, the default is being used."); ! } ! performArrangeImports(doc, endLineDelim, pyEdit.getIndentPrefs().getIndentationString()); } } else { performSimpleSort(doc, endLineDelim, ps.getStartLineIndex(), ps.getEndLineIndex()); } } finally { ! endWrite(doc, session); } } --- 57,82 ---- //let's see if someone wants to make a better implementation in another plugin... List<IOrganizeImports> participants = ExtensionHelper.getParticipants(ExtensionHelper.PYDEV_ORGANIZE_IMPORTS); ! ! for (IOrganizeImports organizeImports : participants) { ! if(!organizeImports.beforePerformArrangeImports(ps, pyEdit)){ ! return; ! } ! } ! ! session = startWrite(doc); ! ! performArrangeImports(doc, endLineDelim, pyEdit.getIndentPrefs().getIndentationString()); ! ! for (IOrganizeImports organizeImports : participants) { ! organizeImports.afterPerformArrangeImports(ps, pyEdit); } } else { + session = startWrite(doc); performSimpleSort(doc, endLineDelim, ps.getStartLineIndex(), ps.getEndLineIndex()); } } finally { ! if(session != null){ ! endWrite(doc, session); ! } } } *************** *** 80,83 **** --- 88,94 ---- } + /** + * Stop a rewrite session + */ private void endWrite(IDocument doc, DocumentRewriteSession session) { if(doc instanceof IDocumentExtension4){ *************** *** 87,90 **** --- 98,104 ---- } + /** + * Starts a rewrite session (keep things in a single undo/redo) + */ private DocumentRewriteSession startWrite(IDocument doc) { if(doc instanceof IDocumentExtension4){ *************** *** 160,210 **** //import from to the imports that should be grouped given its 'from' ! TreeMap<String, List<ImportHandleInfo>> m = new TreeMap<String, List<ImportHandleInfo>>(); List<ImportHandleInfo> importsWithoutFrom = new ArrayList<ImportHandleInfo>(); ! //fill the info ! for (Iterator<Tuple3<Integer, String, ImportHandle>> iter = list.iterator(); iter.hasNext();) { ! Tuple3<Integer, String, ImportHandle> element = iter.next(); ! ! List<ImportHandleInfo> importInfo = element.o3.getImportInfo(); ! for (ImportHandleInfo importHandleInfo : importInfo) { ! String fromImportStr = importHandleInfo.getFromImportStr(); ! if(fromImportStr == null){ ! importsWithoutFrom.add(importHandleInfo); ! }else{ ! List<ImportHandleInfo> lst = m.get(fromImportStr); ! if(lst == null){ ! lst = new ArrayList<ImportHandleInfo>(); ! m.put(fromImportStr, lst); ! } ! lst.add(importHandleInfo); ! } ! } ! } ! //preferences for multiline imports boolean multilineImports = ImportsPreferencesPage.getMultilineImports(); ! int maxCols = 80; ! if(multilineImports){ ! if(PydevPlugin.getDefault() != null){ ! IPreferenceStore chainedPrefStore = PydevPlugin.getChainedPrefStore(); ! maxCols = chainedPrefStore.getInt(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_PRINT_MARGIN_COLUMN); ! } ! }else{ ! maxCols = Integer.MAX_VALUE; ! } ! //preferences for how to break imports ! String breakIportMode = ImportsPreferencesPage.getBreakIportMode(); ! boolean breakWithParenthesis = true; ! if(!breakIportMode.equals(ImportsPreferencesPage.BREAK_IMPORTS_MODE_PARENTHESIS)){ ! breakWithParenthesis = false; ! } ! ! Set<Entry<String, List<ImportHandleInfo>>> entrySet = m.entrySet(); for (Entry<String, List<ImportHandleInfo>> entry : entrySet) { --- 174,191 ---- //import from to the imports that should be grouped given its 'from' ! TreeMap<String, List<ImportHandleInfo>> importsWithFrom = new TreeMap<String, List<ImportHandleInfo>>(); List<ImportHandleInfo> importsWithoutFrom = new ArrayList<ImportHandleInfo>(); ! fillImportStructures(list, importsWithFrom, importsWithoutFrom); //preferences for multiline imports boolean multilineImports = ImportsPreferencesPage.getMultilineImports(); ! int maxCols = getMaxCols(multilineImports); //preferences for how to break imports ! boolean breakWithParenthesis = getBreakImportsWithParenthesis(); ! Set<Entry<String, List<ImportHandleInfo>>> entrySet = importsWithFrom.entrySet(); for (Entry<String, List<ImportHandleInfo>> entry : entrySet) { *************** *** 213,231 **** ArrayList<Tuple<String, String>> importsAndNoComments = new ArrayList<Tuple<String, String>>(); ! for (ImportHandleInfo v : entry.getValue()) { ! List<String> importedStr = v.getImportedStr(); ! List<String> commentsForImports = v.getCommentsForImports(); ! for(int i=0;i<importedStr.size();i++){ ! String importedString = importedStr.get(i); ! String comment = commentsForImports.get(i); ! if(comment.length() > 0){ ! importsAndComments.add(new Tuple<String, String>(importedString, comment)); ! }else{ ! importsAndNoComments.add(new Tuple<String, String>(importedString, comment)); ! } ! } ! } ! ! --- 194,198 ---- ArrayList<Tuple<String, String>> importsAndNoComments = new ArrayList<Tuple<String, String>>(); ! fillImportFromInfo(entry, importsAndComments, importsAndNoComments); *************** *** 307,327 **** } ! //now, write the regular imports (no wrapping or tabbing here) ! for(ImportHandleInfo info:importsWithoutFrom){ ! ! List<String> importedStr = info.getImportedStr(); ! List<String> commentsForImports = info.getCommentsForImports(); ! for(int i=0;i<importedStr.size();i++){ ! all.append("import "); ! String importedString = importedStr.get(i); ! String comment = commentsForImports.get(i); ! all.append(importedString); ! if(comment.length() > 0){ ! all.append(" "); ! all.append(comment); ! } ! all.append(endLineDelim); ! } ! } } --- 274,278 ---- } ! writeImportsWithoutFrom(endLineDelim, all, importsWithoutFrom); } *************** *** 331,334 **** --- 282,386 ---- } + + /** + * Write the imports that don't have a 'from' in the beggining (regular imports) + */ + private static void writeImportsWithoutFrom(String endLineDelim, StringBuffer all, + List<ImportHandleInfo> importsWithoutFrom) { + //now, write the regular imports (no wrapping or tabbing here) + for(ImportHandleInfo info:importsWithoutFrom){ + + List<String> importedStr = info.getImportedStr(); + List<String> commentsForImports = info.getCommentsForImports(); + for(int i=0;i<importedStr.size();i++){ + all.append("import "); + String importedString = importedStr.get(i); + String comment = commentsForImports.get(i); + all.append(importedString); + if(comment.length() > 0){ + all.append(" "); + all.append(comment); + } + all.append(endLineDelim); + } + } + } + + /** + * Fills the lists passed based on the entry set, so that imports that have comments are contained in a list + * and imports without comments in another. + */ + private static void fillImportFromInfo(Entry<String, List<ImportHandleInfo>> entry, + ArrayList<Tuple<String, String>> importsAndComments, ArrayList<Tuple<String, String>> importsAndNoComments) { + for (ImportHandleInfo v : entry.getValue()) { + List<String> importedStr = v.getImportedStr(); + List<String> commentsForImports = v.getCommentsForImports(); + for(int i=0;i<importedStr.size();i++){ + String importedString = importedStr.get(i); + String comment = commentsForImports.get(i); + if(comment.length() > 0){ + importsAndComments.add(new Tuple<String, String>(importedString, comment)); + }else{ + importsAndNoComments.add(new Tuple<String, String>(importedString, comment)); + } + } + } + } + + /** + * @return true if the imports should be split with parenthesis (instead of escaping) + */ + private static boolean getBreakImportsWithParenthesis() { + String breakIportMode = ImportsPreferencesPage.getBreakIportMode(); + boolean breakWithParenthesis = true; + if(!breakIportMode.equals(ImportsPreferencesPage.BREAK_IMPORTS_MODE_PARENTHESIS)){ + breakWithParenthesis = false; + } + return breakWithParenthesis; + } + + /** + * @return the maximum number of columns that may be available in a line. + */ + private static int getMaxCols(boolean multilineImports) { + int maxCols = 80; + if(multilineImports){ + if(PydevPlugin.getDefault() != null){ + IPreferenceStore chainedPrefStore = PydevPlugin.getChainedPrefStore(); + maxCols = chainedPrefStore.getInt(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_PRINT_MARGIN_COLUMN); + } + }else{ + maxCols = Integer.MAX_VALUE; + } + return maxCols; + } + + /** + * Fills the import structure passed, so that the imports from will be grouped by the 'from' part and the regular + * imports will be in a separate list. + */ + private static void fillImportStructures(List<Tuple3<Integer, String, ImportHandle>> list, + TreeMap<String, List<ImportHandleInfo>> importsWithFrom, List<ImportHandleInfo> importsWithoutFrom) { + //fill the info + for (Iterator<Tuple3<Integer, String, ImportHandle>> iter = list.iterator(); iter.hasNext();) { + Tuple3<Integer, String, ImportHandle> element = iter.next(); + + List<ImportHandleInfo> importInfo = element.o3.getImportInfo(); + for (ImportHandleInfo importHandleInfo : importInfo) { + String fromImportStr = importHandleInfo.getFromImportStr(); + if(fromImportStr == null){ + importsWithoutFrom.add(importHandleInfo); + }else{ + List<ImportHandleInfo> lst = importsWithFrom.get(fromImportStr); + if(lst == null){ + lst = new ArrayList<ImportHandleInfo>(); + importsWithFrom.put(fromImportStr, lst); + } + lst.add(importHandleInfo); + } + } + } + } + /** * Performs a simple sort without taking into account the actual contents of the selection (aside from lines Index: IOrganizeImports.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/actions/IOrganizeImports.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** IOrganizeImports.java 25 Feb 2006 00:14:23 -0000 1.2 --- IOrganizeImports.java 2 Jun 2008 00:10:31 -0000 1.3 *************** *** 7,19 **** import org.python.pydev.editor.PyEdit; public interface IOrganizeImports { /** ! * This function should organize the imports in the current pyedit. * * @param ps this is the selection (contains the doc) * @param pyEdit this is the edit */ ! void performArrangeImports(PySelection ps, PyEdit pyEdit); } --- 7,39 ---- import org.python.pydev.editor.PyEdit; + /** + * Interface that makes it possible for clients to interact with the organize imports feature, so that they + * can change how it actually behaves. + * + * @author Fabio + */ public interface IOrganizeImports { /** ! * This function is called just before the actual organize imports function is called. ! * ! * @param ps this is the selection (contains the doc) ! * @param pyEdit this is the edit ! * ! * @return true if the organize imports should proceed, and false if the organize imports should not proceed ! * (so, false cancels the default organize imports) ! * ! * @note this function is always called within a write session in the document (because it should seem as a single ! * operation for the user -- which has a single undo). ! */ ! boolean beforePerformArrangeImports(PySelection ps, PyEdit pyEdit); ! ! /** ! * Called right after the whole import process is done. * * @param ps this is the selection (contains the doc) * @param pyEdit this is the edit */ ! void afterPerformArrangeImports(PySelection ps, PyEdit pyEdit); } |