use of org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument in project webtools.sourceediting by eclipse.
the class JSPedCSSModelParser method insertUnknownImport.
private CSSNodeImpl insertUnknownImport(IStructuredDocumentRegion region) {
CSSModelCreationContext creationContext = getCreationContext();
CSSNodeImpl parent = creationContext.getTargetNode();
ICSSDocument sheet = parent.getOwnerDocument();
String text = region.getText();
Pattern pattern = Pattern.compile(" ");
String[] strs = pattern.split(text);
String hrefValue = null;
for (int i = 0; i < strs.length; i++) {
String hrefStr = "file=\"";
if (strs[i].startsWith(hrefStr)) {
int hrefStr_length = hrefStr.length();
// minus 1 to avoid quote?
int hrefValue_length = strs[i].length() - 1;
if (hrefValue_length > hrefStr_length) {
hrefValue = strs[i].substring(hrefStr_length, hrefValue_length);
} else {
/*
* ISSUE: this handles cases where, e.g. "file=" has no
* subsequent 'value' ... and from code in insertStructuredDocumentRegion
* I believe should return null, rather than empty string, but, this may
* need some fine tuning eventually.
*/
hrefValue = null;
}
break;
}
}
if (hrefValue == null) {
return null;
}
JSPCSSImportRuleImpl rule = new JSPCSSImportRuleImpl();
rule.setOwnerDocument(sheet);
rule.appendChild((CSSNodeImpl) sheet.createMediaList());
rule.setRangeStructuredDocumentRegion(region, region);
if (!isUpdateContextActive()) {
// Attribute(ICSSImportRule.HREF, hrefValue);
rule.setHref(hrefValue);
}
// insert to tree
if (!isUpdateContextActive() && parent != null) {
// propagateRangePreInsert(sheet, rule);
CSSNodeImpl next = creationContext.getNextNode();
if (next != null) {
((CSSNodeImpl) sheet).insertBefore(rule, next);
} else {
((CSSNodeImpl) sheet).appendChild(rule);
}
}
// creationContext.setTargetNode(rule);
return rule;
}
use of org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument in project webtools.sourceediting by eclipse.
the class CSSCompletionProposalComputer method computeCompletionProposals.
/**
* @see org.eclipse.wst.sse.ui.contentassist.ICompletionProposalComputer#computeCompletionProposals(org.eclipse.wst.sse.ui.contentassist.CompletionProposalInvocationContext, org.eclipse.core.runtime.IProgressMonitor)
*/
public List computeCompletionProposals(CompletionProposalInvocationContext context, IProgressMonitor monitor) {
ITextViewer viewer = context.getViewer();
int documentPosition = context.getInvocationOffset();
IndexedRegion indexedNode = ContentAssistUtils.getNodeAt(viewer, documentPosition);
IDOMNode xNode = null;
IDOMNode parent = null;
CSSProposalArranger arranger = null;
// If there is a selected region, we'll need to replace the text
ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
// if(indexedNode == null) return new ICompletionProposal[0];
if (indexedNode instanceof IDOMNode) {
xNode = (IDOMNode) indexedNode;
parent = (IDOMNode) xNode.getParentNode();
}
// case
if ((xNode != null) && xNode.getNodeName().equalsIgnoreCase(HTML40Namespace.ElementName.STYLE)) {
// now we know the cursor is in a <style> tag w/out region
IStructuredModel cssModel = getCSSModel(xNode);
if (cssModel != null) {
// adjust offsets for embedded style
int offset = documentPosition;
int pos = 0;
IndexedRegion keyIndexedNode = cssModel.getIndexedRegion(pos);
if (keyIndexedNode == null) {
keyIndexedNode = (IndexedRegion) ((ICSSModel) cssModel).getDocument();
}
arranger = new CSSProposalArranger(pos, (ICSSNode) keyIndexedNode, offset, selection.getLength(), (char) 0);
}
} else if ((parent != null) && parent.getNodeName().equalsIgnoreCase(HTML40Namespace.ElementName.STYLE)) {
// now we know the cursor is in a <style> tag with a region
// use the parent because that will be the <style> tag
IStructuredModel cssModel = getCSSModel(parent);
if (cssModel != null) {
// adjust offsets for embedded style
int offset = indexedNode.getStartOffset();
int pos = documentPosition - offset;
IndexedRegion keyIndexedNode = cssModel.getIndexedRegion(pos);
if (keyIndexedNode == null) {
keyIndexedNode = (IndexedRegion) ((ICSSModel) cssModel).getDocument();
}
arranger = new CSSProposalArranger(pos, (ICSSNode) keyIndexedNode, offset, selection.getLength(), (char) 0);
}
} else if (indexedNode instanceof IDOMNode) {
IDOMNode domNode = ((IDOMNode) indexedNode);
// get model for node w/ style attribute
IStructuredModel cssModel = getCSSModel(domNode);
if (cssModel != null) {
// adjust offsets for embedded style
int textRegionStartOffset = getTextRegionStartOffset(domNode, documentPosition);
int pos = documentPosition - textRegionStartOffset;
char quote = (char) 0;
try {
quote = context.getDocument().get(textRegionStartOffset, 1).charAt(0);
} catch (BadLocationException e) {
Logger.logException("error getting quote character", e);
}
// get css indexed region
IndexedRegion cssIndexedNode = cssModel.getIndexedRegion(pos);
if (cssIndexedNode == null) {
cssIndexedNode = (IndexedRegion) ((ICSSModel) cssModel).getDocument();
}
if (cssIndexedNode instanceof ICSSNode) {
// inline style for a tag, not embedded
arranger = new CSSProposalArranger(pos, (ICSSNode) cssIndexedNode, textRegionStartOffset, selection.getLength(), quote);
}
}
} else if (indexedNode instanceof ICSSNode) {
// when editing external CSS using CSS Designer, ICSSNode is passed.
ICSSDocument cssdoc = ((ICSSNode) indexedNode).getOwnerDocument();
if (cssdoc != null) {
IStructuredModel cssModel = cssdoc.getModel();
if (cssModel != null) {
IndexedRegion keyIndexedNode = cssModel.getIndexedRegion(documentPosition);
if (keyIndexedNode == null) {
keyIndexedNode = (IndexedRegion) ((ICSSModel) cssModel).getDocument();
}
if (keyIndexedNode instanceof ICSSNode) {
// inline style for a tag, not embedded
arranger = new CSSProposalArranger(documentPosition, (ICSSNode) keyIndexedNode, 0, selection.getLength(), (char) 0);
}
}
}
} else if ((indexedNode == null) && ContentAssistUtils.isViewerEmpty(viewer)) {
// the top of empty CSS Document
IStructuredModel cssModel = null;
try {
cssModel = StructuredModelManager.getModelManager().getExistingModelForRead(viewer.getDocument());
if (cssModel instanceof ICSSModel) {
IndexedRegion keyIndexedNode = cssModel.getIndexedRegion(documentPosition);
if (keyIndexedNode == null) {
keyIndexedNode = (IndexedRegion) ((ICSSModel) cssModel).getDocument();
}
if (keyIndexedNode instanceof ICSSNode) {
// inline style for a tag, not embedded
arranger = new CSSProposalArranger(documentPosition, (ICSSNode) keyIndexedNode, 0, (char) 0);
}
}
} finally {
if (cssModel != null)
cssModel.releaseFromRead();
}
}
ICompletionProposal[] proposals = new ICompletionProposal[0];
if (arranger != null) {
proposals = arranger.getProposals();
ICompletionProposal[] newfileproposals = new ICompletionProposal[0];
ICompletionProposal[] anyproposals = new ICompletionProposal[0];
// add end tag if parent is not closed
ICompletionProposal endTag = XMLContentAssistUtilities.computeXMLEndTagProposal(viewer, documentPosition, indexedNode, HTML40Namespace.ElementName.STYLE, SharedXMLEditorPluginImageHelper.IMG_OBJ_TAG_GENERIC);
// add the additional proposals
int additionalLength = newfileproposals.length + anyproposals.length;
additionalLength = (endTag != null) ? ++additionalLength : additionalLength;
if (additionalLength > 0) {
ICompletionProposal[] plusOnes = new ICompletionProposal[proposals.length + additionalLength];
int appendPos = proposals.length;
// add end tag proposal
if (endTag != null) {
System.arraycopy(proposals, 0, plusOnes, 1, proposals.length);
plusOnes[0] = endTag;
++appendPos;
} else {
System.arraycopy(proposals, 0, plusOnes, 0, proposals.length);
}
// add items in newfileproposals
for (int i = 0; i < newfileproposals.length; ++i) {
plusOnes[appendPos + i] = newfileproposals[i];
}
// add items in anyproposals
appendPos = appendPos + newfileproposals.length;
for (int i = 0; i < anyproposals.length; ++i) {
plusOnes[appendPos + i] = anyproposals[i];
}
proposals = plusOnes;
}
}
return Arrays.asList(proposals);
}
use of org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument in project webtools.sourceediting by eclipse.
the class CSSContentAssistProcessor method computeCompletionProposals.
/**
* Return a list of proposed code completions based on the specified
* location within the document that corresponds to the current cursor
* position within the text-editor control.
*
* @param documentPosition
* a location within the document
* @return an array of code-assist items
*/
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentPosition) {
IndexedRegion indexedNode = ContentAssistUtils.getNodeAt(viewer, documentPosition + fDocumentOffset);
IDOMNode xNode = null;
IDOMNode parent = null;
CSSProposalArranger arranger = null;
boolean isEmptyDocument = false;
// If there is a selected region, we'll need to replace the text
ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
boolean selected = (selection != null && selection.getText() != null && selection.getText().trim().length() > 0);
// if(indexedNode == null) return new ICompletionProposal[0];
if (indexedNode instanceof IDOMNode) {
xNode = (IDOMNode) indexedNode;
parent = (IDOMNode) xNode.getParentNode();
}
// case
if ((xNode != null) && xNode.getNodeName().equalsIgnoreCase(HTML40Namespace.ElementName.STYLE)) {
// now we know the cursor is in a <style> tag w/out region
IStructuredModel cssModel = getCSSModel(xNode);
if (cssModel != null) {
// adjust offsets for embedded style
int offset = documentPosition + fDocumentOffset;
int pos = 0;
IndexedRegion keyIndexedNode = cssModel.getIndexedRegion(pos);
if (keyIndexedNode == null) {
keyIndexedNode = (IndexedRegion) ((ICSSModel) cssModel).getDocument();
}
arranger = new CSSProposalArranger(pos, (ICSSNode) keyIndexedNode, offset, (char) 0, selected);
}
} else if ((parent != null) && parent.getNodeName().equalsIgnoreCase(HTML40Namespace.ElementName.STYLE)) {
// now we know the cursor is in a <style> tag with a region
// use the parent because that will be the <style> tag
IStructuredModel cssModel = getCSSModel(parent);
if (cssModel != null) {
// adjust offsets for embedded style
int offset = indexedNode.getStartOffset();
int pos = documentPosition - offset;
IndexedRegion keyIndexedNode = cssModel.getIndexedRegion(pos);
if (keyIndexedNode == null) {
keyIndexedNode = (IndexedRegion) ((ICSSModel) cssModel).getDocument();
}
arranger = new CSSProposalArranger(pos, (ICSSNode) keyIndexedNode, offset, (char) 0, selected);
}
} else if (indexedNode instanceof IDOMNode) {
// get model for node w/ style attribute
IStructuredModel cssModel = getCSSModel((IDOMNode) indexedNode);
if (cssModel != null) {
IndexedRegion keyIndexedNode = cssModel.getIndexedRegion(documentPosition - fDocumentOffset);
if (keyIndexedNode == null) {
keyIndexedNode = (IndexedRegion) ((ICSSModel) cssModel).getDocument();
}
if (keyIndexedNode instanceof ICSSNode) {
// inline style for a tag, not embedded
arranger = new CSSProposalArranger(documentPosition, (ICSSNode) keyIndexedNode, fDocumentOffset, fQuote, selected);
}
}
} else if (indexedNode instanceof ICSSNode) {
// when editing external CSS using CSS Designer, ICSSNode is
// passed.
ICSSDocument cssdoc = ((ICSSNode) indexedNode).getOwnerDocument();
if (cssdoc != null) {
IStructuredModel cssModel = cssdoc.getModel();
if (cssModel != null) {
IndexedRegion keyIndexedNode = cssModel.getIndexedRegion(documentPosition - fDocumentOffset);
if (keyIndexedNode == null) {
keyIndexedNode = (IndexedRegion) ((ICSSModel) cssModel).getDocument();
}
if (keyIndexedNode instanceof ICSSNode) {
// inline style for a tag, not embedded
arranger = new CSSProposalArranger(documentPosition, (ICSSNode) keyIndexedNode, fDocumentOffset, fQuote, selected);
}
}
}
} else if ((indexedNode == null) && isViewerEmpty(viewer)) {
isEmptyDocument = true;
// the top of empty CSS Document
IStructuredModel cssModel = null;
try {
cssModel = StructuredModelManager.getModelManager().getExistingModelForRead(viewer.getDocument());
if (cssModel instanceof ICSSModel) {
IndexedRegion keyIndexedNode = cssModel.getIndexedRegion(documentPosition - fDocumentOffset);
if (keyIndexedNode == null) {
keyIndexedNode = (IndexedRegion) ((ICSSModel) cssModel).getDocument();
}
if (keyIndexedNode instanceof ICSSNode) {
// inline style for a tag, not embedded
arranger = new CSSProposalArranger(documentPosition, (ICSSNode) keyIndexedNode, fDocumentOffset, fQuote);
}
}
} finally {
if (cssModel != null)
cssModel.releaseFromRead();
}
}
ICompletionProposal[] proposals = new ICompletionProposal[0];
if (arranger != null) {
fDocumentOffset = 0;
proposals = arranger.getProposals();
ICompletionProposal[] newfileproposals = new ICompletionProposal[0];
ICompletionProposal[] anyproposals = new ICompletionProposal[0];
// add template proposals
if (getTemplateCompletionProcessor() != null) {
if (isEmptyDocument) {
getTemplateCompletionProcessor().setContextType(TemplateContextTypeIdsCSS.NEW);
newfileproposals = getTemplateCompletionProcessor().computeCompletionProposals(viewer, documentPosition);
}
getTemplateCompletionProcessor().setContextType(TemplateContextTypeIdsCSS.ALL);
anyproposals = getTemplateCompletionProcessor().computeCompletionProposals(viewer, documentPosition);
}
// add end tag if parent is not closed
ICompletionProposal endTag = XMLContentAssistUtilities.computeXMLEndTagProposal(viewer, documentPosition, indexedNode, HTML40Namespace.ElementName.STYLE, SharedXMLEditorPluginImageHelper.IMG_OBJ_TAG_GENERIC);
// add the additional proposals
int additionalLength = newfileproposals.length + anyproposals.length;
additionalLength = (endTag != null) ? ++additionalLength : additionalLength;
if (additionalLength > 0) {
ICompletionProposal[] plusOnes = new ICompletionProposal[proposals.length + additionalLength];
int appendPos = proposals.length;
// add end tag proposal
if (endTag != null) {
System.arraycopy(proposals, 0, plusOnes, 1, proposals.length);
plusOnes[0] = endTag;
++appendPos;
} else {
System.arraycopy(proposals, 0, plusOnes, 0, proposals.length);
}
// add items in newfileproposals
for (int i = 0; i < newfileproposals.length; ++i) {
plusOnes[appendPos + i] = newfileproposals[i];
}
// add items in anyproposals
appendPos = appendPos + newfileproposals.length;
for (int i = 0; i < anyproposals.length; ++i) {
plusOnes[appendPos + i] = anyproposals[i];
}
proposals = plusOnes;
}
}
return proposals;
}
use of org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument in project webtools.sourceediting by eclipse.
the class CSSProposalGeneratorForAtmarkRule method getCandidateImportRule.
/**
*/
private CSSCACandidate getCandidateImportRule() {
// check content model
CSSMetaModelUtil util = new CSSMetaModelUtil(fContext.getMetaModel());
if (!util.collectNodesByType(CSSMMNode.TYPE_IMPORT_RULE).hasNext()) {
return null;
}
// charset and import can precede import rule.
int offset = fContext.getCursorPos();
if (0 < offset) {
SelectionCollector trav = new SelectionCollector();
trav.setRegion(0, offset - 1);
trav.apply(fContext.getModel().getDocument());
Iterator i = trav.getSelectedNodes().iterator();
while (i.hasNext()) {
Object obj = i.next();
if (obj instanceof ICSSNode && !(obj instanceof ICSSDocument || obj instanceof ICSSCharsetRule || obj instanceof ICSSImportRule)) {
return null;
}
}
}
int cursorPos = 0;
String ident = (fUseUpperCase) ? IMPORT.toUpperCase() : IMPORT.toLowerCase();
StringBuffer buf = new StringBuffer();
buf.append(ident);
// $NON-NLS-1$
buf.append(" ");
cursorPos = buf.length();
StringAndOffset sao;
sao = generateURI();
buf.append(sao.fString);
cursorPos += sao.fOffset;
sao = generateSemicolon();
buf.append(sao.fString);
String text = buf.toString();
if (isMatch(text)) {
CSSCACandidate item = new CSSCACandidate();
item.setReplacementString(buf.toString());
item.setCursorPosition(cursorPos);
item.setDisplayString(ident);
item.setImageType(CSSImageType.RULE_IMPORT);
return item;
} else {
return null;
}
}
use of org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument in project webtools.sourceediting by eclipse.
the class CSSMediaRuleTest method testCreateRule.
public void testCreateRule() {
ICSSStyleSheet sheet = getStyleSheet();
ICSSDocument doc = sheet;
ICSSMediaRule newRule = doc.createCSSMediaRule();
MediaList newList = newRule.getMedia();
newList.appendMedium("media1");
newList.appendMedium("media2");
sheet.insertRuleBefore(newRule, null);
CSSRuleList ruleList = sheet.getCssRules();
CSSRule rule = ruleList.item(0);
assertTrue(rule instanceof CSSMediaRule);
CSSMediaRule mediaRule = (CSSMediaRule) rule;
MediaList mediaList = mediaRule.getMedia();
assertEquals(2, mediaList.getLength());
assertEquals("media1", mediaList.item(0));
assertEquals("media2", mediaList.item(1));
assertEquals("@media media1, media2 {" + FileUtil.commonEOL + "}", mediaRule.getCssText());
}
Aggregations