[86169] added a new type of proposal that can add import statement into a jsp.
diff --git a/bundles/org.eclipse.jst.jsp.ui/src/org/eclipse/jst/jsp/ui/internal/contentassist/AutoImportProposal.java b/bundles/org.eclipse.jst.jsp.ui/src/org/eclipse/jst/jsp/ui/internal/contentassist/AutoImportProposal.java
new file mode 100644
index 0000000..71d648b
--- /dev/null
+++ b/bundles/org.eclipse.jst.jsp.ui/src/org/eclipse/jst/jsp/ui/internal/contentassist/AutoImportProposal.java
@@ -0,0 +1,136 @@
+package org.eclipse.jst.jsp.ui.internal.contentassist;
+
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.ITextViewer;
+import org.eclipse.jface.text.TextUtilities;
+import org.eclipse.jface.text.contentassist.IContextInformation;
+import org.eclipse.jst.jsp.ui.internal.Logger;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.text.edits.InsertEdit;
+import org.eclipse.text.edits.MalformedTreeException;
+import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
+import org.eclipse.wst.sse.core.internal.provisional.StructuredModelManager;
+import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument;
+import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument;
+import org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement;
+import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;
+import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+public class AutoImportProposal extends JSPCompletionProposal {
+
+ // the import string, no quotes or colons
+ String fImportDeclaration;
+
+ public AutoImportProposal(String importDeclaration, String replacementString, int replacementOffset, int replacementLength, int cursorPosition, Image image, String displayString, IContextInformation contextInformation, String additionalProposalInfo, int relevance, boolean updateReplacementLengthOnValidate) {
+ super(replacementString, replacementOffset, replacementLength, cursorPosition, image, displayString, contextInformation, additionalProposalInfo, relevance, updateReplacementLengthOnValidate);
+ setImportDeclaration(importDeclaration);
+ }
+
+ public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
+ super.apply(viewer, trigger, stateMask, offset);
+ addImportDeclaration(viewer);
+ }
+ /**
+ * adds the import declaration to the document in the viewer in the appropriate position
+ * @param viewer
+ */
+ private void addImportDeclaration(ITextViewer viewer) {
+
+ IDocument doc = viewer.getDocument();
+
+ // calculate once and pass along
+ boolean isXml = isXmlFormat(doc);
+
+ int insertPosition = getInsertPosition(doc, isXml);
+ String insertText = createImportDeclaration(doc, isXml);
+ InsertEdit insert = new InsertEdit(insertPosition, insertText);
+ try {
+ insert.apply(doc);
+ }
+ catch (MalformedTreeException e) {
+ Logger.logException(e);
+ }
+ catch (BadLocationException e) {
+ Logger.logException(e);
+ }
+
+ // make sure the cursor position after is correct
+ setCursorPosition(getCursorPosition() + insertText.length());
+ }
+ /**
+ *
+ * @param doc
+ * @param isXml
+ * @return position after <jsp:root> if xml, otherwise 0
+ */
+ private int getInsertPosition(IDocument doc, boolean isXml) {
+ int pos = 0;
+ if(isXml) {
+ // insert after root
+ IStructuredModel sModel = StructuredModelManager.getModelManager().getExistingModelForRead(doc);
+ try {
+ if(sModel != null) {
+ if(sModel instanceof IDOMModel) {
+ IDOMDocument documentNode = ((IDOMModel)sModel).getDocument();
+ Node docElement = documentNode.getDocumentElement();
+ if(docElement != null && docElement instanceof IDOMElement) {
+ pos = ((IDOMElement)docElement).getFirstStructuredDocumentRegion().getEndOffset();
+ }
+ }
+ }
+ }
+ finally {
+ if(sModel != null)
+ sModel.releaseFromRead();
+ }
+ }
+ return pos;
+ }
+ /**
+ *
+ * @param doc
+ * @return true if this document is xml-jsp syntax, otherwise false
+ */
+ private boolean isXmlFormat(IDocument doc) {
+ boolean isXml = false;
+ IStructuredModel sModel = StructuredModelManager.getModelManager().getExistingModelForRead(doc);
+ try {
+ if(sModel != null) {
+ if(!isXml) {
+ if(sModel instanceof IDOMModel) {
+ IDOMDocument documentNode = ((IDOMModel)sModel).getDocument();
+ Element docElement = documentNode.getDocumentElement();
+ isXml = docElement != null && ((docElement.getNodeName().equals("jsp:root")) || ((((IDOMNode) docElement).getStartStructuredDocumentRegion() == null && ((IDOMNode) docElement).getEndStructuredDocumentRegion() == null))); //$NON-NLS-1$
+ }
+ }
+ }
+ }
+ finally {
+ if(sModel != null)
+ sModel.releaseFromRead();
+ }
+ return isXml;
+ }
+ /**
+ *
+ * @param doc
+ * @param isXml
+ * @return appropriate import declaration string depending if document is xml or not
+ */
+ private String createImportDeclaration(IDocument doc, boolean isXml) {
+ String delim = (doc instanceof IStructuredDocument) ? ((IStructuredDocument)doc).getLineDelimiter() : TextUtilities.getDefaultLineDelimiter(doc);
+ if(isXml)
+ return delim + "<jsp:directive.page import=\""+getImportDeclaration()+"\"/>"; //$NON-NLS-1$ //$NON-NLS-2$
+ return "<%@page import=\"" + getImportDeclaration() + "\"%>" + delim; //$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public String getImportDeclaration() {
+ return fImportDeclaration;
+ }
+ public void setImportDeclaration(String importDeclaration) {
+ fImportDeclaration = importDeclaration;
+ }
+}
diff --git a/bundles/org.eclipse.jst.jsp.ui/src/org/eclipse/jst/jsp/ui/internal/contentassist/JSPProposalCollector.java b/bundles/org.eclipse.jst.jsp.ui/src/org/eclipse/jst/jsp/ui/internal/contentassist/JSPProposalCollector.java
index af7392d..041e59e 100644
--- a/bundles/org.eclipse.jst.jsp.ui/src/org/eclipse/jst/jsp/ui/internal/contentassist/JSPProposalCollector.java
+++ b/bundles/org.eclipse.jst.jsp.ui/src/org/eclipse/jst/jsp/ui/internal/contentassist/JSPProposalCollector.java
@@ -85,39 +85,93 @@
JSPCompletionProposal jspProposal = null;
- // from proposal
- String completion = String.valueOf(proposal.getCompletion());
- String mangledName = getMangledName();
-
- // ignore constructor proposals
- // (they will include mangled servlet name)
- if(mangledName != null && completion.indexOf(mangledName) == -1) {
+ // ignore constructor proposals (they're not relevant for our JSP proposal list)
+ if(!proposal.isConstructor()) {
- // java offset
- int offset = proposal.getReplaceStart();
- // replacement length
- int length = proposal.getReplaceEnd() - offset;
- // translate offset from Java > JSP
- offset = fTranslation.getJspOffset(offset);
- // cursor position after must be calculated
- int positionAfter = calculatePositionAfter(proposal, completion, offset);
+ if(proposal.getKind() == CompletionProposal.TYPE_REF) {
+ String signature = String.valueOf(proposal.getDeclarationSignature());
+ String completion = String.valueOf(proposal.getCompletion());
+ if(completion.contains(signature)) {
+ jspProposal = createAutoImportProposal(proposal);
+ }
+ }
- // from java proposal
- IJavaCompletionProposal javaProposal = super.createJavaCompletionProposal(proposal);
- Image image = javaProposal.getImage();
- String displayString = javaProposal.getDisplayString();
- displayString = fixupDisplayString(displayString);
- IContextInformation contextInformation = javaProposal.getContextInformation();
- String additionalInfo = javaProposal.getAdditionalProposalInfo();
- int relevance = javaProposal.getRelevance();
-
- boolean updateLengthOnValidate = true;
-
- jspProposal = new JSPCompletionProposal(completion, offset, length, positionAfter, image, displayString, contextInformation, additionalInfo, relevance, updateLengthOnValidate);
+ // default behavior
+ if(jspProposal == null)
+ jspProposal = createJspProposal(proposal);
}
return jspProposal;
}
+
+
+ private JSPCompletionProposal createAutoImportProposal(CompletionProposal proposal) {
+
+ JSPCompletionProposal jspProposal = null;
+
+ String signature = new String(proposal.getDeclarationSignature());
+ String completion = new String(proposal.getCompletion());
+
+ // it's fully qualified so we should
+ // add an import statement
+ // create an autoimport proposal
+ String newCompletion = completion.replace(signature + ".", "");
+
+ // java offset
+ int offset = proposal.getReplaceStart();
+ // replacement length
+ int length = proposal.getReplaceEnd() - offset;
+ // translate offset from Java > JSP
+ offset = fTranslation.getJspOffset(offset);
+ // cursor position after must be calculated
+ int positionAfter = calculatePositionAfter(proposal, newCompletion, offset);
+
+ // from java proposal
+ IJavaCompletionProposal javaProposal = super.createJavaCompletionProposal(proposal);
+ proposal.getDeclarationSignature();
+ Image image = javaProposal.getImage();
+ String displayString = javaProposal.getDisplayString();
+ displayString = fixupDisplayString(displayString);
+ IContextInformation contextInformation = javaProposal.getContextInformation();
+ String additionalInfo = javaProposal.getAdditionalProposalInfo();
+ int relevance = javaProposal.getRelevance();
+
+ boolean updateLengthOnValidate = true;
+
+ jspProposal = new AutoImportProposal(completion, newCompletion, offset, length, positionAfter, image, displayString, contextInformation, additionalInfo, relevance, updateLengthOnValidate);
+
+ return jspProposal;
+ }
+
+ private JSPCompletionProposal createJspProposal(CompletionProposal proposal) {
+
+ JSPCompletionProposal jspProposal;
+ String completion = String.valueOf(proposal.getCompletion());
+ // java offset
+ int offset = proposal.getReplaceStart();
+ // replacement length
+ int length = proposal.getReplaceEnd() - offset;
+ // translate offset from Java > JSP
+ offset = fTranslation.getJspOffset(offset);
+ // cursor position after must be calculated
+ int positionAfter = calculatePositionAfter(proposal, completion, offset);
+
+ // from java proposal
+ IJavaCompletionProposal javaProposal = super.createJavaCompletionProposal(proposal);
+ proposal.getDeclarationSignature();
+ Image image = javaProposal.getImage();
+ String displayString = javaProposal.getDisplayString();
+ displayString = fixupDisplayString(displayString);
+ IContextInformation contextInformation = javaProposal.getContextInformation();
+ String additionalInfo = javaProposal.getAdditionalProposalInfo();
+ int relevance = javaProposal.getRelevance();
+
+ boolean updateLengthOnValidate = true;
+
+ jspProposal = new JSPCompletionProposal(completion, offset, length, positionAfter, image, displayString, contextInformation, additionalInfo, relevance, updateLengthOnValidate);
+ return jspProposal;
+ }
+
/**
* Cacluates the where the cursor should be after applying this proposal.
* eg. method(|) if the method proposal chosen had params.