PageRenderTime 134ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/org.python.pydev.refactoring/src/org/python/pydev/refactoring/core/edit/AbstractTextEdit.java

https://github.com/MrGreen123/Pydev
Java | 106 lines | 76 code | 23 blank | 7 comment | 4 complexity | 4feca37a13f39f768d2d1383e70287d3 MD5 | raw file
  1. /*
  2. * Copyright (C) 2006, 2007 Dennis Hunziker, Ueli Kistler
  3. * Copyright (C) 2007 Reto Schuettel, Robin Stocker
  4. *
  5. * IFS Institute for Software, HSR Rapperswil, Switzerland
  6. *
  7. */
  8. package org.python.pydev.refactoring.core.edit;
  9. import org.eclipse.text.edits.TextEdit;
  10. import org.python.pydev.core.MisconfigurationException;
  11. import org.python.pydev.parser.jython.SimpleNode;
  12. import org.python.pydev.parser.jython.ast.factory.AdapterPrefs;
  13. import org.python.pydev.parser.jython.ast.factory.NodeHelper;
  14. import org.python.pydev.parser.jython.ast.factory.PyAstFactory;
  15. import org.python.pydev.parser.prettyprinterv2.MakeAstValidForPrettyPrintingVisitor;
  16. import org.python.pydev.refactoring.ast.adapters.AbstractScopeNode;
  17. import org.python.pydev.refactoring.ast.adapters.IASTNodeAdapter;
  18. import org.python.pydev.refactoring.ast.adapters.ModuleAdapter;
  19. import org.python.pydev.refactoring.ast.visitors.rewriter.Rewriter;
  20. import org.python.pydev.refactoring.core.request.IExtractMethodRefactoringRequest;
  21. import org.python.pydev.refactoring.core.request.IRefactoringRequest;
  22. public abstract class AbstractTextEdit {
  23. private static final String WHITESPACE = " ";
  24. private static final String REPLACE_PATTERN = "\\r\\n|\\n|\\r";
  25. protected ModuleAdapter moduleAdapter;
  26. protected IASTNodeAdapter<? extends SimpleNode> offsetAdapter;
  27. protected NodeHelper nodeHelper;
  28. protected AdapterPrefs adapterPrefs;
  29. protected PyAstFactory astFactory;
  30. private AbstractScopeNode<?> scopeAdapter;
  31. public AbstractTextEdit(IRefactoringRequest req) {
  32. this.moduleAdapter = req.getOffsetNode().getModule();
  33. this.offsetAdapter = req.getOffsetNode();
  34. if(req instanceof IExtractMethodRefactoringRequest){
  35. this.scopeAdapter = ((IExtractMethodRefactoringRequest)req).getScopeAdapter();
  36. }
  37. this.nodeHelper = new NodeHelper(req.getAdapterPrefs());
  38. this.adapterPrefs = req.getAdapterPrefs();
  39. this.astFactory = new PyAstFactory(this.nodeHelper.getAdapterPrefs());
  40. }
  41. protected abstract SimpleNode getEditNode() throws MisconfigurationException;
  42. public abstract TextEdit getEdit() throws MisconfigurationException;
  43. protected String getFormattedNode() throws MisconfigurationException {
  44. SimpleNode node = getEditNode().createCopy();
  45. try{
  46. MakeAstValidForPrettyPrintingVisitor.makeValid(node);
  47. }catch(Exception e){
  48. throw new RuntimeException(e);
  49. }
  50. String source = Rewriter.createSourceFromAST(node, this.adapterPrefs);
  51. return getIndentedSource(node, source, getIndent());
  52. }
  53. private String getIndentedSource(SimpleNode node, String source, int indent) {
  54. StringBuilder indented = new StringBuilder();
  55. String indentation = getIndentation(indent);
  56. if(nodeHelper.isFunctionDef(node)){
  57. indented.append(this.adapterPrefs.endLineDelim);
  58. }
  59. indented.append(indentation);
  60. source = source.replaceAll(REPLACE_PATTERN, this.adapterPrefs.endLineDelim + indentation);
  61. source = source.trim();
  62. indented.append(source);
  63. indented.append(this.adapterPrefs.endLineDelim);
  64. if(nodeHelper.isFunctionDef(node)){
  65. indented.append(this.adapterPrefs.endLineDelim);
  66. }
  67. return indented.toString();
  68. }
  69. protected String getIndentation(int indent) {
  70. StringBuilder buf = new StringBuilder();
  71. for(int i = 0; i < indent; i++){
  72. buf.append(WHITESPACE);
  73. }
  74. return buf.toString();
  75. }
  76. public abstract int getOffsetStrategy();
  77. public int getOffset() {
  78. return moduleAdapter.getOffset(offsetAdapter, getOffsetStrategy(), this.scopeAdapter);
  79. }
  80. public int getIndent() {
  81. return offsetAdapter.getNodeBodyIndent();
  82. }
  83. }