Search in sources :

Example 11 with TypedPosition

use of org.eclipse.jface.text.TypedPosition in project eclipse.platform.text by eclipse.

the class FastPartitioner method initialize.

/**
 * Performs the initial partitioning of the partitioner's document.
 * <p>
 * May be extended by subclasses.
 * </p>
 */
protected void initialize() {
    fIsInitialized = true;
    clearPositionCache();
    fScanner.setRange(fDocument, 0, fDocument.getLength());
    try {
        IToken token = fScanner.nextToken();
        while (!token.isEOF()) {
            String contentType = getTokenContentType(token);
            if (isSupportedContentType(contentType)) {
                TypedPosition p = new TypedPosition(fScanner.getTokenOffset(), fScanner.getTokenLength(), contentType);
                fDocument.addPosition(fPositionCategory, p);
            }
            token = fScanner.nextToken();
        }
    } catch (BadLocationException x) {
    // cannot happen as offsets come from scanner
    } catch (BadPositionCategoryException x) {
    // cannot happen if document has been connected before
    }
}
Also used : TypedPosition(org.eclipse.jface.text.TypedPosition) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 12 with TypedPosition

use of org.eclipse.jface.text.TypedPosition in project eclipse.platform.text by eclipse.

the class FastPartitioner method computePartitioning.

/**
 * {@inheritDoc}
 * <p>
 * May be replaced or extended by subclasses.
 * </p>
 */
@Override
public ITypedRegion[] computePartitioning(int offset, int length, boolean includeZeroLengthPartitions) {
    checkInitialization();
    List<TypedRegion> list = new ArrayList<>();
    try {
        int endOffset = offset + length;
        Position[] category = getPositions();
        TypedPosition previous = null, current = null;
        int start, end, gapOffset;
        Position gap = new Position(0);
        int startIndex = getFirstIndexEndingAfterOffset(category, offset);
        int endIndex = getFirstIndexStartingAfterOffset(category, endOffset);
        for (int i = startIndex; i < endIndex; i++) {
            current = (TypedPosition) category[i];
            gapOffset = (previous != null) ? previous.getOffset() + previous.getLength() : 0;
            gap.setOffset(gapOffset);
            gap.setLength(current.getOffset() - gapOffset);
            if ((includeZeroLengthPartitions && overlapsOrTouches(gap, offset, length)) || (gap.getLength() > 0 && gap.overlapsWith(offset, length))) {
                start = Math.max(offset, gapOffset);
                end = Math.min(endOffset, gap.getOffset() + gap.getLength());
                list.add(new TypedRegion(start, end - start, IDocument.DEFAULT_CONTENT_TYPE));
            }
            if (current.overlapsWith(offset, length)) {
                start = Math.max(offset, current.getOffset());
                end = Math.min(endOffset, current.getOffset() + current.getLength());
                list.add(new TypedRegion(start, end - start, current.getType()));
            }
            previous = current;
        }
        if (previous != null) {
            gapOffset = previous.getOffset() + previous.getLength();
            gap.setOffset(gapOffset);
            gap.setLength(fDocument.getLength() - gapOffset);
            if ((includeZeroLengthPartitions && overlapsOrTouches(gap, offset, length)) || (gap.getLength() > 0 && gap.overlapsWith(offset, length))) {
                start = Math.max(offset, gapOffset);
                end = Math.min(endOffset, fDocument.getLength());
                list.add(new TypedRegion(start, end - start, IDocument.DEFAULT_CONTENT_TYPE));
            }
        }
        if (list.isEmpty())
            list.add(new TypedRegion(offset, length, IDocument.DEFAULT_CONTENT_TYPE));
    } catch (BadPositionCategoryException ex) {
        // Make sure we clear the cache
        clearPositionCache();
    } catch (RuntimeException ex) {
        // Make sure we clear the cache
        clearPositionCache();
        throw ex;
    }
    TypedRegion[] result = new TypedRegion[list.size()];
    list.toArray(result);
    return result;
}
Also used : TypedPosition(org.eclipse.jface.text.TypedPosition) Position(org.eclipse.jface.text.Position) TypedPosition(org.eclipse.jface.text.TypedPosition) ArrayList(java.util.ArrayList) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) ITypedRegion(org.eclipse.jface.text.ITypedRegion) TypedRegion(org.eclipse.jface.text.TypedRegion)

Example 13 with TypedPosition

use of org.eclipse.jface.text.TypedPosition in project eclipse.platform.text by eclipse.

the class RuleBasedPartitioner method computePartitioning.

@Override
public ITypedRegion[] computePartitioning(int offset, int length, boolean includeZeroLengthPartitions) {
    List<TypedRegion> list = new ArrayList<>();
    try {
        int endOffset = offset + length;
        Position[] category = fDocument.getPositions(fPositionCategory);
        TypedPosition previous = null, current = null;
        int start, end, gapOffset;
        Position gap = null;
        for (Position element : category) {
            current = (TypedPosition) element;
            gapOffset = (previous != null) ? previous.getOffset() + previous.getLength() : 0;
            gap = new Position(gapOffset, current.getOffset() - gapOffset);
            if ((includeZeroLengthPartitions || gap.getLength() > 0) && gap.overlapsWith(offset, length)) {
                start = Math.max(offset, gapOffset);
                end = Math.min(endOffset, gap.getOffset() + gap.getLength());
                list.add(new TypedRegion(start, end - start, IDocument.DEFAULT_CONTENT_TYPE));
            }
            if (current.overlapsWith(offset, length)) {
                start = Math.max(offset, current.getOffset());
                end = Math.min(endOffset, current.getOffset() + current.getLength());
                list.add(new TypedRegion(start, end - start, current.getType()));
            }
            previous = current;
        }
        if (previous != null) {
            gapOffset = previous.getOffset() + previous.getLength();
            gap = new Position(gapOffset, fDocument.getLength() - gapOffset);
            if ((includeZeroLengthPartitions || gap.getLength() > 0) && ((includeZeroLengthPartitions && offset + length == gapOffset && gap.length == 0) || gap.overlapsWith(offset, length))) {
                start = Math.max(offset, gapOffset);
                end = Math.min(endOffset, fDocument.getLength());
                list.add(new TypedRegion(start, end - start, IDocument.DEFAULT_CONTENT_TYPE));
            }
        }
        if (list.isEmpty())
            list.add(new TypedRegion(offset, length, IDocument.DEFAULT_CONTENT_TYPE));
    } catch (BadPositionCategoryException x) {
    }
    TypedRegion[] result = new TypedRegion[list.size()];
    list.toArray(result);
    return result;
}
Also used : TypedPosition(org.eclipse.jface.text.TypedPosition) Position(org.eclipse.jface.text.Position) TypedPosition(org.eclipse.jface.text.TypedPosition) ArrayList(java.util.ArrayList) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) ITypedRegion(org.eclipse.jface.text.ITypedRegion) TypedRegion(org.eclipse.jface.text.TypedRegion)

Example 14 with TypedPosition

use of org.eclipse.jface.text.TypedPosition in project eclipse.platform.text by eclipse.

the class RuleBasedPartitioner method getPartition.

/*
	 * @see IDocumentPartitioner#getPartition
	 */
@Override
public ITypedRegion getPartition(int offset) {
    try {
        Position[] category = fDocument.getPositions(fPositionCategory);
        if (category == null || category.length == 0)
            return new TypedRegion(0, fDocument.getLength(), IDocument.DEFAULT_CONTENT_TYPE);
        int index = fDocument.computeIndexInCategory(fPositionCategory, offset);
        if (index < category.length) {
            TypedPosition next = (TypedPosition) category[index];
            if (offset == next.offset)
                return new TypedRegion(next.getOffset(), next.getLength(), next.getType());
            if (index == 0)
                return new TypedRegion(0, next.offset, IDocument.DEFAULT_CONTENT_TYPE);
            TypedPosition previous = (TypedPosition) category[index - 1];
            if (previous.includes(offset))
                return new TypedRegion(previous.getOffset(), previous.getLength(), previous.getType());
            int endOffset = previous.getOffset() + previous.getLength();
            return new TypedRegion(endOffset, next.getOffset() - endOffset, IDocument.DEFAULT_CONTENT_TYPE);
        }
        TypedPosition previous = (TypedPosition) category[category.length - 1];
        if (previous.includes(offset))
            return new TypedRegion(previous.getOffset(), previous.getLength(), previous.getType());
        int endOffset = previous.getOffset() + previous.getLength();
        return new TypedRegion(endOffset, fDocument.getLength() - endOffset, IDocument.DEFAULT_CONTENT_TYPE);
    } catch (BadPositionCategoryException x) {
    } catch (BadLocationException x) {
    }
    return new TypedRegion(0, fDocument.getLength(), IDocument.DEFAULT_CONTENT_TYPE);
}
Also used : TypedPosition(org.eclipse.jface.text.TypedPosition) Position(org.eclipse.jface.text.Position) TypedPosition(org.eclipse.jface.text.TypedPosition) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) ITypedRegion(org.eclipse.jface.text.ITypedRegion) TypedRegion(org.eclipse.jface.text.TypedRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 15 with TypedPosition

use of org.eclipse.jface.text.TypedPosition in project webtools.sourceediting by eclipse.

the class FormattingStrategyJSDT method format.

/*
	 * @see org.eclipse.jface.text.formatter.ContextBasedFormattingStrategy#format()
	 */
public void format() {
    super.format();
    final IStructuredDocument document = (IStructuredDocument) fDocuments.removeFirst();
    final TypedPosition partition = (TypedPosition) fPartitions.removeFirst();
    if (document != null) {
        // calculate the indent of the leading <script> tag because we need to add that indent level to the JS indent level
        IStructuredDocumentRegion scriptTagStartRegion = document.getRegionAtCharacterOffset(partition.offset - 1);
        // $NON-NLS-1$
        String scriptRegionIndent = "";
        if (scriptTagStartRegion != null) {
            try {
                int scriptRegionIndentLevel = getIndentOfLine(document, document.getLineOfOffset(scriptTagStartRegion.getStartOffset())).length();
                scriptRegionIndent = getIndentationString(getPreferences(), scriptRegionIndentLevel);
                this.startIndentLevel += scriptRegionIndentLevel;
            } catch (BadLocationException e) {
                // $NON-NLS-1$
                Logger.logException("Could not calculate starting indent of the script region, using 0", e);
            }
        }
        String lineDelim = TextUtilities.getDefaultLineDelimiter(document);
        try {
            // get the JS text from the document (not translated)
            String jsTextNotTranslated = document.get(partition.getOffset(), partition.getLength());
            String originalText = jsTextNotTranslated;
            // deal with getting the JS text and unwrapping it from any <!-- //--> statements
            String preText = "";
            String postText = lineDelim + scriptRegionIndent;
            // find and remove start comment tag if it's there
            // $NON-NLS-1$
            Pattern startPattern = Pattern.compile("(\\A(\\s*<!--.*(" + lineDelim + ")?))");
            Matcher matcher = startPattern.matcher(jsTextNotTranslated);
            if (matcher.find()) {
                preText = lineDelim + scriptRegionIndent + matcher.group().trim();
                // $NON-NLS-1$
                jsTextNotTranslated = matcher.replaceFirst("");
            }
            // find and remove end comment tag if it's there
            matcher = END_PATTERN.matcher(jsTextNotTranslated);
            if (matcher.find()) {
                // $NON-NLS-1$
                jsTextNotTranslated = matcher.replaceFirst("");
                postText = lineDelim + scriptRegionIndent + matcher.group().trim() + postText;
            }
            /*
				 * replace the text in the document with the non-translated JS
				 * text but without HTML leading and trailing comments
				 */
            int scriptLength = jsTextNotTranslated.length();
            TextEdit replaceEdit = null;
            if (scriptLength != originalText.length()) {
                replaceEdit = new ReplaceEdit(partition.getOffset(), partition.getLength(), jsTextNotTranslated);
                replaceEdit.apply(document);
            }
            // translate the web page without the script "wrapping"
            IJsTranslation translation = getTranslation(document);
            String jsTextTranslated = translation.getJsText();
            /*
				 * Set a default replace text that is the original contents
				 * with a new line and proper indentation in front
				 */
            String replaceText = lineDelim + getIndentationString(getPreferences(), startIndentLevel) + jsTextNotTranslated;
            int javaScriptOffset = ((JsTranslation) translation).getJavaScriptOffset(partition.getOffset());
            // known range, proceed
            if (javaScriptOffset >= 0) {
                // format the translated text
                TextEdit edit = CodeFormatterUtil.format2(CodeFormatter.K_JAVASCRIPT_UNIT, jsTextTranslated, javaScriptOffset, scriptLength, startIndentLevel, lineDelim, getPreferences());
                IDocument jsDoc = new Document(jsTextTranslated);
                if (edit != null) {
                    /*
						 * Put the original (possibly not JS) text back into the doc
						 * to which we're applying the edit
						 */
                    if (translation instanceof JsTranslation) {
                        IJsTranslator translator = ((JsTranslation) translation).getTranslator();
                        if (translator instanceof JsTranslator) {
                            Region[] regions = ((JsTranslator) translator).getGeneratedRanges();
                            Arrays.sort(regions, new Comparator() {

                                public int compare(Object o1, Object o2) {
                                    return ((IRegion) o1).getOffset() - ((IRegion) o2).getOffset();
                                }
                            });
                            /*
								 * for each web page range representing content needing replacements, replace it with the
								 * original web page's text
								 */
                            for (int r = 0; r < regions.length; ++r) {
                                int javascriptOffset = ((JsTranslation) translation).getJavaScriptOffset(regions[r].getOffset());
                                if (javascriptOffset > 0) {
                                    jsDoc.replace(javascriptOffset, regions[r].getLength(), document.get(regions[r].getOffset(), regions[r].getLength()));
                                }
                            }
                        }
                    }
                    edit.apply(jsDoc);
                    replaceText = lineDelim + getIndentationString(getPreferences(), startIndentLevel) + (jsDoc.get(edit.getOffset(), edit.getLength())).trim();
                } else {
                    /*
						 * Revert changes (it may still appear dirty, though,
						 * because of the above edits having been applied)
						 */
                    replaceEdit = new ReplaceEdit(partition.getOffset(), scriptLength, originalText);
                    replaceEdit.apply(document);
                    return;
                }
            }
            // apply edit to html doc using the formated translated text and the possible leading and trailing html comments
            replaceText = preText + replaceText + postText;
            replaceEdit = new ReplaceEdit(partition.getOffset(), scriptLength, replaceText);
            replaceEdit.apply(document);
        } catch (BadLocationException e) {
            Logger.logException(e);
        }
    }
}
Also used : IJsTranslation(org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation) Pattern(java.util.regex.Pattern) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) Matcher(java.util.regex.Matcher) JsTranslator(org.eclipse.wst.jsdt.web.core.javascript.JsTranslator) IJsTranslator(org.eclipse.wst.jsdt.web.core.javascript.IJsTranslator) JsTranslation(org.eclipse.wst.jsdt.web.core.javascript.JsTranslation) IJsTranslation(org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) BasicStructuredDocument(org.eclipse.wst.sse.core.internal.text.BasicStructuredDocument) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IJsTranslator(org.eclipse.wst.jsdt.web.core.javascript.IJsTranslator) IRegion(org.eclipse.jface.text.IRegion) Comparator(java.util.Comparator) TypedPosition(org.eclipse.jface.text.TypedPosition) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) BadLocationException(org.eclipse.jface.text.BadLocationException) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

TypedPosition (org.eclipse.jface.text.TypedPosition)38 BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)26 BadLocationException (org.eclipse.jface.text.BadLocationException)23 Position (org.eclipse.jface.text.Position)19 ITypedRegion (org.eclipse.jface.text.ITypedRegion)13 TypedRegion (org.eclipse.jface.text.TypedRegion)12 ArrayList (java.util.ArrayList)7 IDocument (org.eclipse.jface.text.IDocument)7 IRegion (org.eclipse.jface.text.IRegion)5 IToken (org.eclipse.jface.text.rules.IToken)4 TextEdit (org.eclipse.text.edits.TextEdit)3 List (java.util.List)2 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)2 XMLNode (com.amalto.workbench.widgets.xmlviewer.model.XMLNode)1 IOException (java.io.IOException)1 Comparator (java.util.Comparator)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 CoreException (org.eclipse.core.runtime.CoreException)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1