Skip to content

Commit

Permalink
Fixing CodeGeneratorController’s retrieving Editor.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmorcinek committed Sep 27, 2014
1 parent 2cd6cae commit 43f9910
Showing 1 changed file with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.TextEditor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.morcinek.android.codegenerator.CodeGenerator;
Expand All @@ -12,6 +13,7 @@
import javax.xml.xpath.XPathExpressionException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
* Copyright 2014 Tomasz Morcinek. All rights reserved.
Expand All @@ -25,22 +27,33 @@ public CodeGeneratorController(String templateName, ResourceProvidersFactory res
}

public String generateCode(Project project, VirtualFile file, Editor editor) throws ParserConfigurationException, SAXException, XPathExpressionException, IOException {
editor = getTextEditor(editor, project);
return codeGenerator.produceCode(getEditorContents(editor), file.getName());
return codeGenerator.produceCode(getContents(project, editor, file), file.getName());
}

private Editor getTextEditor(Editor editor, Project project) {
private InputStream getContents(Project project, Editor editor, VirtualFile file) throws IOException {
editor = getEditor(project, editor, file);
if (editor != null) {
return new ByteArrayInputStream(getText(editor).getBytes());
} else {
return file.getInputStream();
}
}

private Editor getEditor(Project project, Editor editor, VirtualFile file) {
if (editor == null) {
return FileEditorManager.getInstance(project).getSelectedTextEditor();
TextEditor textEditor = getTextEditor(project, file);
if (textEditor != null) {
return textEditor.getEditor();
}
}
return editor;
}

private ByteArrayInputStream getEditorContents(Editor editor) {
return new ByteArrayInputStream(getEditorText(editor).getBytes());
private TextEditor getTextEditor(Project project, VirtualFile file) {
return (TextEditor) FileEditorManager.getInstance(project).getSelectedEditor(file);
}

private String getEditorText(Editor editor) {
private String getText(Editor editor) {
return editor.getDocument().getText();
}
}

0 comments on commit 43f9910

Please sign in to comment.