HTML&XML code style

This commit is contained in:
Olesya Smirnova
2005-01-31 22:51:30 +03:00
parent 72a2f7c536
commit 7ba954220c
4 changed files with 118 additions and 72 deletions
@@ -33,6 +33,7 @@ package com.intellij.application.options;
import com.intellij.ide.highlighter.HighlighterFactory;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
@@ -63,16 +64,21 @@ public abstract class CodeStyleAbstractPanel {
CodeStyleSettings.WRAP_AS_NEEDED,
CodeStyleSettings.WRAP_ON_EVERY_ITEM,
CodeStyleSettings.WRAP_ALWAYS};
private long myLastDocumentModificationStamp;
private String myTextToReformat = null;
public CodeStyleAbstractPanel(CodeStyleSettings settings) {
mySettings = settings;
myEditor = createEditor();
}
protected static final Editor createEditor() {
protected final Editor createEditor() {
EditorFactory editorFactory = EditorFactory.getInstance();
Document editorDocument = editorFactory.createDocument("");
EditorEx editor = (EditorEx)editorFactory.createViewer(editorDocument);
myTextToReformat = getPreviewText();
Document editorDocument = editorFactory.createDocument(myTextToReformat);
EditorEx editor = (EditorEx)editorFactory.createEditor(editorDocument);
myLastDocumentModificationStamp = editor.getDocument().getModificationStamp();
EditorSettings editorSettings = editor.getSettings();
editorSettings.setWhitespacesShown(true);
@@ -81,11 +87,13 @@ public abstract class CodeStyleAbstractPanel {
editorSettings.setFoldingOutlineShown(false);
editorSettings.setAdditionalColumnsCount(0);
editorSettings.setAdditionalLinesCount(1);
editorSettings.setRightMargin(mySettings.RIGHT_MARGIN);
EditorColorsScheme scheme = editor.getColorsScheme();
scheme.setColor(EditorColors.CARET_ROW_COLOR, null);
editor.setHighlighter(HighlighterFactory.createXMLHighlighter(scheme));
return editor;
}
@@ -93,15 +101,29 @@ public abstract class CodeStyleAbstractPanel {
if (!myShouldUpdatePreview) {
return;
}
final String text = getPreviewText();
if (myLastDocumentModificationStamp != myEditor.getDocument().getModificationStamp()) {
myTextToReformat = myEditor.getDocument().getText();
}
CommandProcessor.getInstance().executeCommand(ProjectManager.getInstance().getDefaultProject(),
new Runnable() {
public void run() {
replaceText();
}
}, null, null);
myLastDocumentModificationStamp = myEditor.getDocument().getModificationStamp();
}
private void replaceText() {
final Project project = ProjectManager.getInstance().getDefaultProject();
final PsiManager manager = PsiManager.getInstance(project);
ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
PsiElementFactory factory = manager.getElementFactory();
try {
PsiFile psiFile = factory.createFileFromText("a." + getFileType().getDefaultExtension(), text);
PsiFile psiFile = factory.createFileFromText("a." + getFileType().getDefaultExtension(), myTextToReformat);
CodeStyleSettings clone = (CodeStyleSettings)mySettings.clone();
apply(clone);
@@ -113,7 +135,6 @@ public abstract class CodeStyleAbstractPanel {
myEditor.getSettings().setTabSize(clone.getTabSize(getFileType()));
Document document = myEditor.getDocument();
document.replaceString(0, document.getTextLength(), psiFile.getText());
}
catch (IncorrectOperationException e) {
LOG.error(e);
@@ -1,46 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.intellij.application.options.CodeStyleHtmlPanel">
<grid id="677fa" binding="myPanel" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="77227" binding="myPanel" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="4" y="9" width="552" height="416"/>
<xy x="4" y="8" width="552" height="417"/>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="9aabb" row-count="7" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<xy id="3dbb2" binding="myPreviewPanel" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="0" y="0" width="332" height="307"/>
<xy x="349" y="0" width="203" height="417"/>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3"/>
</constraints>
<properties/>
<border type="etched" title="Preview"/>
<children/>
</xy>
<grid id="ac5b7" row-count="8" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="0" y="0" width="339" height="417"/>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="f230c" class="javax.swing.JLabel">
<vspacer id="9276c">
<constraints>
<xy x="0" y="3" width="80" height="14"/>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0"/>
<xy x="246" y="307" width="11" height="110"/>
<grid row="7" column="2" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2"/>
</constraints>
<properties>
<text value="Keep blank lines:"/>
</properties>
</component>
<component id="18182" class="javax.swing.JTextField" binding="myKeepBlankLines">
<constraints>
<xy x="124" y="0" width="40" height="20"/>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0">
<preferred-size width="40" height="-1"/>
</grid>
</constraints>
<properties>
<columns value="3"/>
</properties>
</component>
</vspacer>
<component id="d4efa" class="javax.swing.JLabel">
<constraints>
<xy x="0" y="29" width="80" height="14"/>
<xy x="0" y="31" width="84" height="14"/>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0"/>
</constraints>
<properties>
@@ -49,7 +45,7 @@
</component>
<component id="8bb88" class="javax.swing.JComboBox" binding="myWrapAttributes">
<constraints>
<xy x="124" y="25" width="208" height="23"/>
<xy x="125" y="27" width="214" height="22"/>
<grid row="1" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="2" anchor="8" fill="1"/>
</constraints>
<properties>
@@ -58,20 +54,20 @@
</component>
<hspacer id="b6d89">
<constraints>
<xy x="164" y="4" width="168" height="11"/>
<xy x="165" y="5" width="174" height="11"/>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1"/>
</constraints>
</hspacer>
<component id="96c04" class="javax.swing.JComboBox" binding="myTextWrapping">
<constraints>
<xy x="124" y="53" width="208" height="23"/>
<xy x="125" y="54" width="214" height="22"/>
<grid row="2" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="2" anchor="8" fill="1"/>
</constraints>
<properties/>
</component>
<component id="77e79" class="javax.swing.JLabel">
<constraints>
<xy x="0" y="57" width="53" height="14"/>
<xy x="0" y="58" width="57" height="14"/>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0"/>
</constraints>
<properties>
@@ -81,7 +77,7 @@
<grid id="6063" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="124" y="81" width="208" height="76"/>
<xy x="125" y="81" width="214" height="76"/>
<grid row="3" column="1" row-span="3" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3"/>
</constraints>
<properties/>
@@ -89,7 +85,7 @@
<children>
<component id="f4575" class="javax.swing.JCheckBox" binding="mySpacesAroundEquality">
<constraints>
<xy x="6" y="20" width="135" height="22"/>
<xy x="6" y="20" width="136" height="22"/>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0"/>
</constraints>
<properties>
@@ -98,7 +94,7 @@
</component>
<component id="8f9a9" class="javax.swing.JCheckBox" binding="mySpacesAroundTagName">
<constraints>
<xy x="6" y="48" width="108" height="22"/>
<xy x="6" y="48" width="109" height="22"/>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0"/>
</constraints>
<properties>
@@ -109,7 +105,7 @@
</grid>
<component id="8a13c" class="javax.swing.JCheckBox" binding="myAlignAttributes">
<constraints>
<xy x="0" y="81" width="98" height="22"/>
<xy x="0" y="81" width="99" height="22"/>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0"/>
</constraints>
<properties>
@@ -118,7 +114,7 @@
</component>
<component id="165dc" class="javax.swing.JCheckBox" binding="myAlignText">
<constraints>
<xy x="0" y="108" width="71" height="22"/>
<xy x="0" y="108" width="72" height="22"/>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0"/>
</constraints>
<properties>
@@ -127,7 +123,7 @@
</component>
<component id="959da" class="javax.swing.JCheckBox" binding="myKeepWhiteSpaces">
<constraints>
<xy x="0" y="135" width="114" height="22"/>
<xy x="0" y="135" width="115" height="22"/>
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0"/>
</constraints>
<properties>
@@ -137,7 +133,7 @@
<grid id="ca5bb" row-count="6" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="0" y="162" width="332" height="145"/>
<xy x="0" y="162" width="339" height="145"/>
<grid row="6" column="0" row-span="1" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3"/>
</constraints>
<properties/>
@@ -145,7 +141,7 @@
<children>
<component id="fe20d" class="javax.swing.JLabel">
<constraints>
<xy x="0" y="3" width="110" height="14"/>
<xy x="0" y="3" width="114" height="14"/>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0"/>
</constraints>
<properties>
@@ -154,7 +150,7 @@
</component>
<component id="99b92" class="javax.swing.JTextField" binding="myInsertNewLineTagNames">
<constraints>
<xy x="133" y="0" width="199" height="20"/>
<xy x="137" y="0" width="202" height="20"/>
<grid row="0" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1"/>
</grid>
@@ -163,7 +159,7 @@
</component>
<component id="160eb" class="javax.swing.JLabel">
<constraints>
<xy x="0" y="28" width="120" height="14"/>
<xy x="0" y="28" width="124" height="14"/>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0"/>
</constraints>
<properties>
@@ -172,7 +168,7 @@
</component>
<component id="2084f" class="javax.swing.JTextField" binding="myRemoveNewLineTagNames">
<constraints>
<xy x="133" y="25" width="199" height="20"/>
<xy x="137" y="25" width="202" height="20"/>
<grid row="1" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1"/>
</grid>
@@ -181,7 +177,7 @@
</component>
<component id="93fdf" class="javax.swing.JLabel">
<constraints>
<xy x="0" y="53" width="122" height="14"/>
<xy x="0" y="53" width="126" height="14"/>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0"/>
</constraints>
<properties>
@@ -190,7 +186,7 @@
</component>
<component id="65b85" class="javax.swing.JTextField" binding="myDoNotAlignChildrenTagNames">
<constraints>
<xy x="133" y="50" width="199" height="20"/>
<xy x="137" y="50" width="202" height="20"/>
<grid row="2" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1"/>
</grid>
@@ -199,7 +195,7 @@
</component>
<component id="9646b" class="javax.swing.JLabel">
<constraints>
<xy x="0" y="103" width="72" height="14"/>
<xy x="0" y="103" width="76" height="14"/>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0"/>
</constraints>
<properties>
@@ -208,7 +204,7 @@
</component>
<component id="121a0" class="javax.swing.JLabel">
<constraints>
<xy x="0" y="78" width="104" height="14"/>
<xy x="0" y="78" width="108" height="14"/>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0"/>
</constraints>
<properties>
@@ -217,7 +213,7 @@
</component>
<component id="547a0" class="javax.swing.JTextField" binding="myDoNotAlignChildrenMinSize">
<constraints>
<xy x="133" y="75" width="151" height="20"/>
<xy x="137" y="75" width="150" height="20"/>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1"/>
</grid>
@@ -226,7 +222,7 @@
</component>
<component id="a3eda" class="javax.swing.JLabel">
<constraints>
<xy x="294" y="78" width="38" height="14"/>
<xy x="297" y="78" width="42" height="14"/>
<grid row="3" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0"/>
</constraints>
<properties>
@@ -235,7 +231,7 @@
</component>
<component id="18980" class="javax.swing.JTextField" binding="myTextElementsTagNames">
<constraints>
<xy x="133" y="100" width="199" height="20"/>
<xy x="137" y="100" width="202" height="20"/>
<grid row="4" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1"/>
</grid>
@@ -244,7 +240,7 @@
</component>
<component id="c84da" class="javax.swing.JTextField" binding="myKeepWhiteSpacesTagNames">
<constraints>
<xy x="133" y="125" width="199" height="20"/>
<xy x="137" y="125" width="202" height="20"/>
<grid row="5" column="1" row-span="1" col-span="2" vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1"/>
</grid>
@@ -253,7 +249,7 @@
</component>
<component id="1ade9" class="javax.swing.JLabel">
<constraints>
<xy x="0" y="128" width="123" height="14"/>
<xy x="0" y="128" width="127" height="14"/>
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0"/>
</constraints>
<properties>
@@ -262,24 +258,28 @@
</component>
</children>
</grid>
<component id="c888a" class="javax.swing.JCheckBox" binding="myShouldKeepBlankLines">
<constraints>
<xy x="0" y="0" width="102" height="22"/>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0"/>
</constraints>
<properties>
<text value="Keep blank lines"/>
</properties>
</component>
<component id="18182" class="javax.swing.JTextField" binding="myKeepBlankLines">
<constraints>
<xy x="125" y="1" width="40" height="20"/>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0">
<preferred-size width="40" height="-1"/>
</grid>
</constraints>
<properties>
<columns value="3"/>
</properties>
</component>
</children>
</grid>
<xy id="3dbb2" binding="myPreviewPanel" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="342" y="0" width="210" height="416"/>
<grid row="0" column="1" row-span="2" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3"/>
</constraints>
<properties/>
<border type="etched" title="Preview"/>
<children/>
</xy>
<vspacer id="9276c">
<constraints>
<xy x="160" y="307" width="11" height="109"/>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2"/>
</constraints>
</vspacer>
</children>
</grid>
</form>
@@ -31,10 +31,10 @@
*/
package com.intellij.application.options;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.openapi.util.Comparing;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.StdFileTypes;
import com.intellij.openapi.util.Comparing;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
@@ -63,6 +63,7 @@ public class CodeStyleHtmlPanel extends CodeStyleAbstractPanel {
private JTextField myKeepWhiteSpacesTagNames;
private JTextField myTextElementsTagNames;
private JTextField myDoNotAlignChildrenMinSize;
private JCheckBox myShouldKeepBlankLines;
public CodeStyleHtmlPanel(CodeStyleSettings settings) {
super(settings);
@@ -109,6 +110,13 @@ public class CodeStyleHtmlPanel extends CodeStyleAbstractPanel {
myDoNotAlignChildrenMinSize.getDocument().addDocumentListener(documentListener);
myTextElementsTagNames.getDocument().addDocumentListener(documentListener);
myKeepWhiteSpacesTagNames.getDocument().addDocumentListener(documentListener);
myShouldKeepBlankLines.addActionListener(actionListener);
myShouldKeepBlankLines.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
myKeepBlankLines.setEnabled(myShouldKeepBlankLines.isSelected());
}
});
}
public void apply(CodeStyleSettings settings) {
@@ -127,6 +135,7 @@ public class CodeStyleHtmlPanel extends CodeStyleAbstractPanel {
settings.HTML_DO_NOT_ALIGN_CHILDREN_OF_MIN_SIZE = getIntValue(myDoNotAlignChildrenMinSize);
settings.HTML_TEXT_ELEMENTS = myTextElementsTagNames.getText();
settings.HTML_KEEP_WHITESPACES_INSIDE = myKeepWhiteSpacesTagNames.getText();
settings.HTML_KEEP_LINE_BREAKS = myShouldKeepBlankLines.isSelected();
}
private int getIntValue(JTextField keepBlankLines) {
@@ -147,6 +156,7 @@ public class CodeStyleHtmlPanel extends CodeStyleAbstractPanel {
myKeepWhiteSpaces.setSelected(mySettings.HTML_KEEP_WHITESPACES);
mySpacesAroundTagName.setSelected(mySettings.HTML_SPACE_AROUND_TAG_NAME);
mySpacesAroundEquality.setSelected(mySettings.HTML_SPACE_AROUND_EQUALITY_IN_ATTRINUTE);
myShouldKeepBlankLines.setSelected(mySettings.HTML_KEEP_LINE_BREAKS);
myInsertNewLineTagNames.setText(mySettings.HTML_ELEMENTS_TO_INSERT_NEW_LINE_BEFORE);
myRemoveNewLineTagNames.setText(mySettings.HTML_ELEMENTS_TO_REMOVE_NEW_LINE_BEFORE);
@@ -155,6 +165,7 @@ public class CodeStyleHtmlPanel extends CodeStyleAbstractPanel {
myTextElementsTagNames.setText(mySettings.HTML_TEXT_ELEMENTS);
myKeepWhiteSpacesTagNames.setText(mySettings.HTML_KEEP_WHITESPACES_INSIDE);
myKeepBlankLines.setEnabled(myShouldKeepBlankLines.isSelected());
}
public boolean isModified(CodeStyleSettings settings) {
@@ -213,6 +224,10 @@ public class CodeStyleHtmlPanel extends CodeStyleAbstractPanel {
return true;
}
if (myShouldKeepBlankLines.isSelected() != mySettings.HTML_KEEP_LINE_BREAKS) {
return true;
}
return false;
}
@@ -15,6 +15,7 @@ import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.pom.Navigatable;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
@@ -85,8 +86,17 @@ public class ViewStructureAction extends AnAction implements TreeActionsOwner{
}
PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
if (psiFile == null) {
presentation.setEnabled(false);
return;
}
final VirtualFile virtualFile = psiFile.getVirtualFile();
presentation.setEnabled((psiFile.getVirtualFile().getFileType().getStructureViewModel(psiFile.getVirtualFile(), project) != null) || AntFileStructureList.canShowFor(psiFile));
if (virtualFile == null) {
presentation.setEnabled(false);
return;
}
presentation.setEnabled((virtualFile.getFileType().getStructureViewModel(virtualFile, project) != null) || AntFileStructureList.canShowFor(psiFile));
}
public void setActionActive(String name, boolean state) {