Lines 1-5
Link Here
|
1 |
/******************************************************************************* |
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2001, 2006 IBM Corporation and others. |
2 |
* Copyright (c) 2001, 2008 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
5 |
* which accompanies this distribution, and is available at |
Lines 22-27
Link Here
|
22 |
import org.eclipse.ui.PlatformUI; |
22 |
import org.eclipse.ui.PlatformUI; |
23 |
import org.eclipse.ui.texteditor.ITextEditor; |
23 |
import org.eclipse.ui.texteditor.ITextEditor; |
24 |
import org.eclipse.ui.texteditor.ITextEditorExtension3; |
24 |
import org.eclipse.ui.texteditor.ITextEditorExtension3; |
|
|
25 |
import org.eclipse.wst.html.ui.internal.HTMLUIPlugin; |
26 |
import org.eclipse.wst.html.ui.internal.preferences.HTMLUIPreferenceNames; |
25 |
import org.eclipse.wst.sse.core.StructuredModelManager; |
27 |
import org.eclipse.wst.sse.core.StructuredModelManager; |
26 |
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; |
28 |
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; |
27 |
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion; |
29 |
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion; |
Lines 51-56
Link Here
|
51 |
if (command.text != null) { |
53 |
if (command.text != null) { |
52 |
smartInsertForComment(command, document, model); |
54 |
smartInsertForComment(command, document, model); |
53 |
smartInsertForEndTag(command, document, model); |
55 |
smartInsertForEndTag(command, document, model); |
|
|
56 |
smartRemoveEndTag(command, document, model); |
54 |
} |
57 |
} |
55 |
} |
58 |
} |
56 |
} |
59 |
} |
Lines 60-65
Link Here
|
60 |
} |
63 |
} |
61 |
} |
64 |
} |
62 |
|
65 |
|
|
|
66 |
private boolean isPreferenceEnabled(String key) { |
67 |
return (key != null && HTMLUIPlugin.getDefault().getPreferenceStore().getBoolean(key)); |
68 |
} |
69 |
|
63 |
private boolean isCommentNode(IDOMNode node) { |
70 |
private boolean isCommentNode(IDOMNode node) { |
64 |
return (node != null && node instanceof IDOMElement && ((IDOMElement) node).isCommentTag()); |
71 |
return (node != null && node instanceof IDOMElement && ((IDOMElement) node).isCommentTag()); |
65 |
} |
72 |
} |
Lines 67-76
Link Here
|
67 |
private boolean isDocumentNode(IDOMNode node) { |
74 |
private boolean isDocumentNode(IDOMNode node) { |
68 |
return (node != null && node.getNodeType() == Node.DOCUMENT_NODE); |
75 |
return (node != null && node.getNodeType() == Node.DOCUMENT_NODE); |
69 |
} |
76 |
} |
|
|
77 |
|
78 |
/** |
79 |
* Attempts to clean up an end-tag if a start-tag is converted into an empty-element |
80 |
* tag (e.g., <node />) and the original element was empty. |
81 |
* |
82 |
* @param command the document command describing the change |
83 |
* @param document the document that will be changed |
84 |
* @param model the model based on the document |
85 |
*/ |
86 |
private void smartRemoveEndTag(DocumentCommand command, IDocument document, IStructuredModel model) { |
87 |
try { |
88 |
// An opening tag is now a self-terminated end-tag |
89 |
if ("/".equals(command.text) && ">".equals(document.get(command.offset, 1)) && isPreferenceEnabled(HTMLUIPreferenceNames.TYPING_REMOVE_END_TAGS)) { //$NON-NLS-1$ //$NON-NLS-2$ |
90 |
IDOMNode node = (IDOMNode) model.getIndexedRegion(command.offset); |
91 |
if (node != null && !node.hasChildNodes()) { |
92 |
IStructuredDocumentRegion region = node.getEndStructuredDocumentRegion(); |
93 |
|
94 |
if (region != null && region.isEnded()) |
95 |
document.replace(region.getStartOffset(), region.getLength(), ""); //$NON-NLS-1$ |
96 |
} |
97 |
} |
98 |
} |
99 |
catch (BadLocationException e) { |
100 |
Logger.logException(e); |
101 |
} |
102 |
} |
70 |
|
103 |
|
71 |
private void smartInsertForComment(DocumentCommand command, IDocument document, IStructuredModel model) { |
104 |
private void smartInsertForComment(DocumentCommand command, IDocument document, IStructuredModel model) { |
72 |
try { |
105 |
try { |
73 |
if (command.text.equals("-") && document.getLength() >= 3 && document.get(command.offset - 3, 3).equals("<!-")) { //$NON-NLS-1$ //$NON-NLS-2$ |
106 |
if (command.text.equals("-") && document.getLength() >= 3 && document.get(command.offset - 3, 3).equals("<!-") && isPreferenceEnabled(HTMLUIPreferenceNames.TYPING_COMPLETE_COMMENTS)) { //$NON-NLS-1$ //$NON-NLS-2$ |
74 |
command.text += " -->"; //$NON-NLS-1$ |
107 |
command.text += " -->"; //$NON-NLS-1$ |
75 |
command.shiftsCaret = false; |
108 |
command.shiftsCaret = false; |
76 |
command.caretOffset = command.offset + 2; |
109 |
command.caretOffset = command.offset + 2; |
Lines 85-91
Link Here
|
85 |
|
118 |
|
86 |
private void smartInsertForEndTag(DocumentCommand command, IDocument document, IStructuredModel model) { |
119 |
private void smartInsertForEndTag(DocumentCommand command, IDocument document, IStructuredModel model) { |
87 |
try { |
120 |
try { |
88 |
if (command.text.equals("/") && document.getLength() >= 1 && document.get(command.offset - 1, 1).equals("<")) { //$NON-NLS-1$ //$NON-NLS-2$ |
121 |
if (command.text.equals("/") && document.getLength() >= 1 && document.get(command.offset - 1, 1).equals("<") && isPreferenceEnabled(HTMLUIPreferenceNames.TYPING_COMPLETE_END_TAGS)) { //$NON-NLS-1$ //$NON-NLS-2$ |
89 |
IDOMNode parentNode = (IDOMNode) ((IDOMNode) model.getIndexedRegion(command.offset - 1)).getParentNode(); |
122 |
IDOMNode parentNode = (IDOMNode) ((IDOMNode) model.getIndexedRegion(command.offset - 1)).getParentNode(); |
90 |
if (isCommentNode(parentNode)) { |
123 |
if (isCommentNode(parentNode)) { |
91 |
// loop and find non comment node parent |
124 |
// loop and find non comment node parent |