private static boolean isInMultiLineStatement(IStructuredDocument document, String checkedLineBeginState, String checkedLineEndState, int checkedOffset, int lineStart, int currLineIndex) { // TODO if in phpdoc or php miltiline,return false; try { char[] line = document.get(lineStart, document.getLineLength(currLineIndex)).toCharArray(); for (int i = 0; i < line.length; i++) { char c = line[i]; if (Character.isWhitespace(c)) { } else { // move line start to first non blank char lineStart += i + 1; break; } } } catch (BadLocationException e) { } TextSequence textSequence = PHPTextSequenceUtilities.getStatement(lineStart, document.getRegionAtCharacterOffset(lineStart), true); if (textSequence != null && isRegionTypeAllowedMultiline( FormatterUtils.getRegionType(document, textSequence.getOriginalOffset(0))) && document.getLineOfOffset(textSequence.getOriginalOffset(0)) < currLineIndex) { return true; } return false; }
/** */ public void replaceSource(IStructuredModel model, int offset, int length, String source) { if (model == null) return; IStructuredDocument structuredDocument = model.getStructuredDocument(); if (structuredDocument == null) return; if (offset >= 0 && length >= 0 && offset + length <= structuredDocument.getLength()) { if (structuredDocument.containsReadOnly(offset, length)) return; if (source == null) source = new String(); // We use 'structuredDocument' as the requester object just so // this and the other // format-related 'repalceText' (in replaceSource) can use the // same requester. // Otherwise, if requester is not identical, // the undo group gets "broken" into multiple pieces based // on the requesters being different. Technically, any unique, // common // requester object would work. structuredDocument.replaceText(structuredDocument, offset, length, source); } }
public IStructuredDocumentRegion getNext() { return getParentDocument().getFirstStructuredDocumentRegion(); }
private String getLineDelimiter(IStructuredDocument doc, int nodeOffset) { int line = doc.getLineOfOffset(nodeOffset); String lineDelimiter = doc.getLineDelimiter(); try { if (line > 0) { lineDelimiter = doc.getLineDelimiter(line - 1); } } catch (BadLocationException e) { // log for now, unless we find reason not to Logger.log(Logger.INFO, e.getMessage()); } // BUG115716: if cannot get line delimiter from current line, just // use default line delimiter if (lineDelimiter == null) lineDelimiter = doc.getLineDelimiter(); return lineDelimiter; }
final IRegion originalLineInfo = document.getLineInformation(lineNumber); final int orginalLineStart = originalLineInfo.getOffset(); final int originalLineLength = originalLineInfo.getLength(); final String lineText = document.get(orginalLineStart, originalLineLength); final IRegion formattedLineInformation = getFormattedLineInformation(originalLineInfo, lineText); final int originalTextEnd = orginalLineStart + originalLineLength; if (formattedTextEnd != originalTextEnd) { document.replace(formattedTextEnd, originalTextEnd - formattedTextEnd, ""); //$NON-NLS-1$ final IStructuredDocumentRegion sdRegion = document.getRegionAtCharacterOffset(formattedLineStart); ITextRegion firstTokenInLine = sdRegion.getRegionAtCharacterOffset(formattedLineStart); int regionStart = sdRegion.getStartOffset(firstTokenInLine); document.getLineOffset(lineNumber)); document.replaceText(sdRegion, orginalLineStart, startingWhiteSpaces, newIndentation);
/** * Determines the structured document region of the place in PHP code where * completion was requested * * @return structured document region or <code>null</code> in case it could * not be determined */ protected IStructuredDocumentRegion determineStructuredDocumentRegion(IStructuredDocument document, int offset) { IStructuredDocumentRegion sdRegion = null; int lastOffset = offset; // find the structured document region: while (sdRegion == null && lastOffset >= 0) { sdRegion = document.getRegionAtCharacterOffset(lastOffset); lastOffset--; } return sdRegion; }
/** * Check if the line contains any non blank chars. */ protected static boolean isBlanks(final IStructuredDocument document, final int startOffset, final int endOffset, final int currentOffset) throws BadLocationException { return document.get(startOffset, endOffset - startOffset).trim().length() == 0 || document.get(startOffset, currentOffset - startOffset).trim().length() == 0; }
/** * This method will replace the string at offset and length with a new * string. If the string to be replaced is the same as the new string, the * string will not be replaced. */ protected void replace(IStructuredDocument structuredDocument, int offset, int length, String string) { try { String structuredDocumentString = structuredDocument.get(offset, length); if (structuredDocumentString.compareTo(string) != 0) structuredDocument.replaceText(structuredDocument, offset, length, string); } catch (BadLocationException e) { // log for now, unless we find reason not to Logger.log(Logger.INFO, e.getMessage()); } }
/** * */ private StructuredDocumentEvent removeText(int start, int length) { StructuredDocumentEvent result = null; IStructuredDocument structuredDocument = fModel.getStructuredDocument(); if (structuredDocument != null) { result = structuredDocument.replaceText(fModel, start, length, new String(""));//$NON-NLS-1$ } return result; }
public void formatModel(IStructuredModel structuredModel) { int start = 0; int length = structuredModel.getStructuredDocument().getLength(); formatModel(structuredModel, start, length); }
public void setStructuredDocument(IStructuredDocument newDocument) { resetNodes(); getStructuredDocumentRegionHandlers().remove(this); if (newDocument != null && newDocument.getParser() instanceof StructuredDocumentRegionParser) { ((StructuredDocumentRegionParser) newDocument.getParser()).addStructuredDocumentRegionHandler(this); } } }
private String getNamedStyleAtOffset(int offset) { // ensure the offset is clean if (offset >= fDocument.getLength()) return getNamedStyleAtOffset(fDocument.getLength() - 1); else if (offset < 0) return getNamedStyleAtOffset(0); IStructuredDocumentRegion documentRegion = fDocument.getFirstStructuredDocumentRegion(); while (documentRegion != null && !documentRegion.containsOffset(offset)) { documentRegion = documentRegion.getNext(); } if (documentRegion != null) { // find the ITextRegion's Context at this offset ITextRegion interest = documentRegion.getRegionAtCharacterOffset(offset); if (interest == null) return null; if (offset > documentRegion.getTextEndOffset(interest)) return null; String regionContext = interest.getType(); if (regionContext == null) return null; // find the named style (internal/selectable name) for that // context String namedStyle = (String) fContextToStyleMap.get(regionContext); if (namedStyle != null) { return namedStyle; } } return null; }
private void setCssValueTextCore(String value) throws DOMException { // use temporary document synchronized (CSSStyleDeclarationImpl.class) { if (sharedStructuredDocument == null) { IDocumentLoader loader = new CSSDocumentLoader(); sharedStructuredDocument = (IStructuredDocument) loader.createNewStructuredDocument(); ((CSSSourceParser) sharedStructuredDocument.getParser()).setParserMode(CSSSourceParser.MODE_DECLARATION_VALUE); } sharedStructuredDocument.set(value); IStructuredDocumentRegion node = sharedStructuredDocument.getFirstStructuredDocumentRegion(); if (node == null) { return; } if (node.getNext() != null) { throw new DOMException(DOMException.INVALID_MODIFICATION_ERR, "");//$NON-NLS-1$ } CSSDeclarationItemParser itemParser = new CSSDeclarationItemParser(getOwnerDocument()); itemParser.setStructuredDocumentTemporary(true); // make a copy of nodelist because setupValues will destroy list ITextRegionList nodeList = new TextRegionListImpl(node.getRegions()); itemParser.setupValues(this, node, nodeList); } }
public int getLineNumber() { return this.node.getStructuredDocument().getLineOfOffset(getOffset()) + 1; }
int offset = (refRule != null) ? refRule.getStartOffset() : doc.getLength(); doc.replaceText(this, offset, 0, rule);
public static void placeMatchingBlanksForStructuredDocument(final IStructuredDocument document, final StringBuilder result, final int lineNumber, final int forOffset) throws BadLocationException { boolean enterKeyPressed = document.getLineDelimiter().equals(result.toString()); int lastNonEmptyLineIndex = getIndentationBaseLine(document, lineNumber, forOffset, false); final int indentationBaseLineIndex = getIndentationBaseLine(document, lineNumber, forOffset, true); final IRegion lastNonEmptyLine = document.getLineInformation(lastNonEmptyLineIndex); final IRegion indentationBaseLine = document.getLineInformation(indentationBaseLineIndex); final String blanks = FormatterUtils.getLineBlanks(document, indentationBaseLine); result.append(blanks); IRegion lineInfo = document.getLineInformation(lastNonEmptyLineIndex); String content = document.get(lineInfo.getOffset(), lineInfo.getLength()); if (content.trim().length() > 0) { break; IRegion lineInfo = document.getLineInformation(lastNonEmptyLineIndex); result.append(FormatterUtils.getLineBlanks(document, lineInfo)); IRegion lineInfo = document.getLineInformation(lastNonEmptyLineIndex); result.append(FormatterUtils.getLineBlanks(document, lineInfo));
public void setStructuredDocument(IStructuredDocument newStructuredDocument) { IStructuredDocument oldStructuredDocument = super.getStructuredDocument(); if (newStructuredDocument == oldStructuredDocument) return; // noting to do if (oldStructuredDocument != null) oldStructuredDocument.removeDocumentChangingListener(this); super.setStructuredDocument(newStructuredDocument); if (newStructuredDocument != null) { if (newStructuredDocument.getLength() > 0) { newModel(new NewDocumentEvent(newStructuredDocument, this)); } newStructuredDocument.addDocumentChangingListener(this); } }
/** */ private String getDelimiter(IDOMModel model) { String delim = null; if (model != null) { IStructuredDocument structuredDocument = model.getStructuredDocument(); if (structuredDocument != null) delim = structuredDocument.getLineDelimiter(); } if (delim == null) delim = "\r\n";//$NON-NLS-1$ return delim; }
static protected StructuredDocumentEvent replaceSource(IDOMModel model, Object requester, int offset, int length, String source) { StructuredDocumentEvent result = null; if (model == null) return result; IStructuredDocument structuredDocument = model.getStructuredDocument(); if (structuredDocument == null) return result; if (source == null) source = new String(); if (structuredDocument.containsReadOnly(offset, length)) return result; if (requester == null) { requester = structuredDocument; } return structuredDocument.replaceText(requester, offset, length, source); }