use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.
the class ConvertToJavaProcessor method performRefactoring.
//private static String
@Override
protected void performRefactoring(@NotNull UsageInfo[] usages) {
final GeneratorClassNameProvider classNameProvider = new GeneratorClassNameProvider();
ExpressionContext context = new ExpressionContext(myProject, myFiles);
final ClassGenerator classGenerator = new ClassGenerator(classNameProvider, new ClassItemGeneratorImpl(context));
for (GroovyFile file : myFiles) {
final PsiClass[] classes = file.getClasses();
StringBuilder builder = new StringBuilder();
boolean first = true;
for (PsiClass aClass : classes) {
classGenerator.writeTypeDefinition(builder, aClass, true, first);
first = false;
builder.append('\n');
}
final Document document = PsiDocumentManager.getInstance(myProject).getDocument(file);
LOG.assertTrue(document != null);
document.setText(builder.toString());
PsiDocumentManager.getInstance(myProject).commitDocument(document);
String fileName = getNewFileName(file);
PsiElement newFile;
try {
newFile = file.setName(fileName);
} catch (final IncorrectOperationException e) {
ApplicationManager.getApplication().invokeLater(() -> Messages.showMessageDialog(myProject, e.getMessage(), RefactoringBundle.message("error.title"), Messages.getErrorIcon()));
return;
}
doPostProcessing(newFile);
}
}
use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.
the class FormSourceCodeGenerator method generate.
public void generate(final VirtualFile formFile) {
myNeedLoadLabelText = false;
myNeedLoadButtonText = false;
final Module module = ModuleUtil.findModuleForFile(formFile, myProject);
if (module == null) {
return;
}
// ensure that new instances of generators are used for every run
ourContainerLayoutCodeGenerators.clear();
ourContainerLayoutCodeGenerators.put(UIFormXmlConstants.LAYOUT_INTELLIJ, new GridLayoutSourceGenerator());
ourContainerLayoutCodeGenerators.put(UIFormXmlConstants.LAYOUT_GRIDBAG, new GridBagLayoutSourceGenerator());
ourContainerLayoutCodeGenerators.put(UIFormXmlConstants.LAYOUT_BORDER, new BorderLayoutSourceGenerator());
ourContainerLayoutCodeGenerators.put(UIFormXmlConstants.LAYOUT_FLOW, new FlowLayoutSourceGenerator());
ourContainerLayoutCodeGenerators.put(UIFormXmlConstants.LAYOUT_CARD, new CardLayoutSourceGenerator());
ourContainerLayoutCodeGenerators.put(UIFormXmlConstants.LAYOUT_FORM, new FormLayoutSourceGenerator());
myErrors.clear();
final PsiPropertiesProvider propertiesProvider = new PsiPropertiesProvider(module);
final Document doc = FileDocumentManager.getInstance().getDocument(formFile);
final LwRootContainer rootContainer;
try {
rootContainer = Utils.getRootContainer(doc.getText(), propertiesProvider);
} catch (AlienFormFileException ignored) {
// ignoring this file
return;
} catch (Exception e) {
myErrors.add(new FormErrorInfo(null, UIDesignerBundle.message("error.cannot.process.form.file", e)));
return;
}
if (rootContainer.getClassToBind() == null) {
// form skipped - no class to bind
return;
}
ErrorAnalyzer.analyzeErrors(module, formFile, null, rootContainer, null);
FormEditingUtil.iterate(rootContainer, new FormEditingUtil.ComponentVisitor<LwComponent>() {
public boolean visit(final LwComponent iComponent) {
final ErrorInfo errorInfo = ErrorAnalyzer.getErrorForComponent(iComponent);
if (errorInfo != null) {
String message;
if (iComponent.getBinding() != null) {
message = UIDesignerBundle.message("error.for.component", iComponent.getBinding(), errorInfo.myDescription);
} else {
message = errorInfo.myDescription;
}
myErrors.add(new FormErrorInfo(iComponent.getId(), message));
}
return true;
}
});
if (myErrors.size() != 0) {
return;
}
try {
_generate(rootContainer, module);
} catch (ClassToBindNotFoundException e) {
// ignore
} catch (CodeGenerationException e) {
myErrors.add(new FormErrorInfo(e.getComponentId(), e.getMessage()));
} catch (IncorrectOperationException e) {
myErrors.add(new FormErrorInfo(null, e.getMessage()));
}
}
use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.
the class UIFormEditor method getState.
@NotNull
public FileEditorState getState(@NotNull final FileEditorStateLevel ignored) {
final Document document = FileDocumentManager.getInstance().getCachedDocument(myFile);
long modificationStamp = document != null ? document.getModificationStamp() : myFile.getModificationStamp();
final ArrayList<RadComponent> selection = FormEditingUtil.getSelectedComponents(myEditor);
final String[] ids = new String[selection.size()];
for (int i = ids.length - 1; i >= 0; i--) {
ids[i] = selection.get(i).getId();
}
return new MyEditorState(modificationStamp, ids);
}
use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.
the class DefaultGenerateElementProvider method generate.
@SuppressWarnings({ "unchecked" })
@Nullable
public T generate(@Nullable final DomElement parent, final Editor editor) {
if (parent == null) {
return null;
}
final List<? extends DomCollectionChildDescription> list = parent.getGenericInfo().getCollectionChildrenDescriptions();
for (DomCollectionChildDescription childDescription : list) {
if (ReflectionUtil.getRawType(childDescription.getType()).isAssignableFrom(myChildElementClass)) {
XmlTag parentTag = parent.getXmlTag();
if (editor != null && parentTag != null) {
int offset = editor.getCaretModel().getOffset();
PsiFile file = parentTag.getContainingFile();
PsiElement psiElement = file.findElementAt(offset);
if (psiElement instanceof PsiWhiteSpace && PsiTreeUtil.isAncestor(parentTag, psiElement, false)) {
Document document = editor.getDocument();
XmlTag childTag = parentTag.createChildTag(childDescription.getXmlElementName(), null, null, true);
String text = childTag.getText();
document.insertString(offset, text);
Project project = editor.getProject();
assert project != null;
PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
documentManager.commitDocument(document);
PsiElement element = file.findElementAt(offset + 1);
T domElement = DomUtil.findDomElement(element, myChildElementClass);
if (domElement != null)
return domElement;
document.deleteString(offset, offset + text.length());
documentManager.commitDocument(document);
}
}
int index = getCollectionIndex(parent, childDescription, editor);
return index < 0 ? (T) childDescription.addValue(parent, myChildElementClass) : (T) childDescription.addValue(parent, myChildElementClass, index);
}
}
return null;
}
use of com.intellij.openapi.editor.Document in project intellij-community by JetBrains.
the class PerspectiveFileEditor method setSelectionInTextEditor.
public void setSelectionInTextEditor(final TextEditor textEditor, final DomElement element) {
if (element != null && element.isValid()) {
final XmlTag tag = element.getXmlTag();
if (tag == null)
return;
final PsiFile file = tag.getContainingFile();
if (file == null)
return;
final Document document = getDocumentManager().getDocument(file);
if (document == null || !document.equals(textEditor.getEditor().getDocument()))
return;
textEditor.getEditor().getCaretModel().moveToOffset(tag.getTextOffset());
textEditor.getEditor().getScrollingModel().scrollToCaret(ScrollType.CENTER);
}
}
Aggregations