Java Code Examples for org.netbeans.editor.Utilities#getRowStartFromLineOffset()

The following examples show how to use org.netbeans.editor.Utilities#getRowStartFromLineOffset() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TagBasedFormatter.java    From netbeans with Apache License 2.0 6 votes vote down vote up
protected int getInitialIndentFromPreviousLine(final BaseDocument doc, final int line) throws BadLocationException {
    
    // get initial indent from the previous line
    int initialIndent = 0;
    
    if (line > 0){
        int lineStart = Utilities.getRowStartFromLineOffset(doc, line);
        int previousNonWhiteLineEnd = Utilities.getFirstNonWhiteBwd(doc, lineStart);
        
        if (previousNonWhiteLineEnd > 0){
            initialIndent = Utilities.getRowIndent(doc, previousNonWhiteLineEnd);
        }
    }
    
    return initialIndent;
}
 
Example 2
Source File: AnnotationView.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private synchronized int getModelToViewImpl(int line) throws BadLocationException {
    int docLines = Utilities.getRowCount(doc);
    
    if (modelToViewCache == null || height != pane.getHeight() || lines != docLines) {
        modelToViewCache = new int[Utilities.getRowCount(doc) + 2];
        lines = Utilities.getRowCount(doc);
        height = pane.getHeight();
    }
    
    if (line >= docLines)
        return -1;
    
    int result = modelToViewCache[line + 1];
    
    if (result == 0) {
        int lineOffset = Utilities.getRowStartFromLineOffset((BaseDocument) pane.getDocument(), line);
        
        modelToViewCache[line + 1] = result = getYFromPos(lineOffset);
    }
    
    if (result == (-1))
        result = 0;
    
    return result;
}
 
Example 3
Source File: AbstractIndenter.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void addLanguageEndLine(List<Line> lines) throws BadLocationException {
    // check what last line of language suggests about next line:
    if (lines.isEmpty()) {
        return;
    }
    Line lastLine = lines.get(lines.size()-1);
    if (lastLine.preliminaryNextLineIndent.isEmpty()) {
        return;
    }
    int lineIndex = lastLine.index+1;
    int offset = Utilities.getRowStartFromLineOffset(getDocument(), lineIndex);
    if (offset == -1) {
        return;
    }
    Line l = generateBasicLine(lineIndex);
    l.indentThisLine = false;
    l.lineIndent = new ArrayList<IndentCommand>(lastLine.preliminaryNextLineIndent);
    lines.add(l);
}
 
Example 4
Source File: TagBasedLexerFormatter.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void markCurrentLanguageLines(BaseDocument doc, TextBounds languageBounds, EmbeddingType[] embeddingType) throws BadLocationException {
    if (languageBounds.getStartPos() == -1){
        return; // only white spaces
    }
    
    int firstLineOfTheLanguageBlock = languageBounds.getStartLine();

    int lineStart = Utilities.getRowStartFromLineOffset(doc, firstLineOfTheLanguageBlock);

    if (Utilities.getFirstNonWhiteFwd(doc, lineStart) < languageBounds.getStartPos()) {
        firstLineOfTheLanguageBlock++;
    }

    for (int i = firstLineOfTheLanguageBlock; i <= languageBounds.getEndLine(); i++) {
        embeddingType[i] = EmbeddingType.CURRENT_LANG;
    }
}
 
Example 5
Source File: AbstractIndenter.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private Line generateBasicLine(int index) throws BadLocationException {
    Line line = new Line();
    line.index = index;
    line.offset = Utilities.getRowStartFromLineOffset(getDocument(), index);
    line.existingLineIndent = IndentUtils.lineIndent(getDocument(), line.offset);
    int nonWS = Utilities.getRowFirstNonWhite(getDocument(), line.offset);
    line.emptyLine = nonWS == -1;
    // if (first-non-whitespace-offset - line-start-offset) is different from 
    // existingLineIndent then line starts with tab characters which will need
    // to be replaced; if line is empty set tabIndentation to true just to make sure
    // possible tabs get replaced:
    line.tabIndentation = nonWS == -1 || line.existingLineIndent != (nonWS - line.offset);
    line.lineStartOffset = line.offset;
    line.lineEndOffset = Utilities.getRowEnd(getDocument(), line.offset);
    line.lineIndent = new ArrayList<IndentCommand>();
    line.lineIndent.add(new IndentCommand(IndentCommand.Type.NO_CHANGE, line.offset, getIndentationSize()));
    line.preliminaryNextLineIndent = new ArrayList<IndentCommand>();
    line.preliminaryNextLineIndent.add(new IndentCommand(IndentCommand.Type.NO_CHANGE, line.offset, getIndentationSize()));

    return line;
}
 
Example 6
Source File: TagBasedLexerFormatter.java    From netbeans with Apache License 2.0 6 votes vote down vote up
protected int getInitialIndentFromPreviousLine(final BaseDocument doc, final int line) throws BadLocationException {

        // get initial indent from the previous line
        int initialIndent = 0;

        if (line > 0) {
            int lineStart = Utilities.getRowStartFromLineOffset(doc, line);
            int previousNonWhiteLineEnd = Utilities.getFirstNonWhiteBwd(doc, lineStart);

            if (previousNonWhiteLineEnd > 0) {
                initialIndent = Utilities.getRowIndent(doc, previousNonWhiteLineEnd);
            }
        }

        return initialIndent;
    }
 
Example 7
Source File: DiffViewManager.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private boolean canRollback(Document doc, Difference diff) {
    if (!(doc instanceof GuardedDocument)) return true;
    GuardedDocument document = (GuardedDocument) doc;
    int start, end;
    if (diff.getType() == Difference.DELETE) {
        start = end = Utilities.getRowStartFromLineOffset(document, diff.getSecondStart());
    } else {
        start = Utilities.getRowStartFromLineOffset(document, diff.getSecondStart() > 0 ? diff.getSecondStart() - 1 : 0);
        end = Utilities.getRowStartFromLineOffset(document, diff.getSecondEnd());
    }
    MarkBlockChain mbc = ((GuardedDocument) document).getGuardedBlockChain();
    return (mbc.compareBlock(start, end) & MarkBlock.OVERLAP) == 0;
}
 
Example 8
Source File: DiffViewManager.java    From netbeans with Apache License 2.0 5 votes vote down vote up
static int getRowStartFromLineOffset(Document doc, int lineIndex) {
    if (doc instanceof BaseDocument) {
        return Utilities.getRowStartFromLineOffset((BaseDocument) doc, lineIndex);
    } else {
        // TODO: find row start from line offet
        Element element = doc.getDefaultRootElement();
        Element line = element.getElement(lineIndex);
        return line.getStartOffset();
    }
}
 
Example 9
Source File: DiffSidebar.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private Point scrollToDifference(Difference diff) {
    int lineStart = diff.getSecondStart() - 1;
    int lineEnd = diff.getSecondEnd() - 1;
    if (lineStart == -1) {
        // the change was delete starting on the first line, show the diff on the next line
        // since in this case the file cannot be empty, 0 index does not throw BLE
        lineStart = 0;
    }
    if (diff.getType() == Difference.DELETE) {
        lineEnd = lineStart;
    }
    try {
        EditorUI editorUI = Utilities.getEditorUI(textComponent);
        int visibleBorder = editorUI.getLineHeight() * 5;
        int startOffset = Utilities.getRowStartFromLineOffset((BaseDocument) textComponent.getDocument(), lineStart);
        int endOffset = Utilities.getRowStartFromLineOffset((BaseDocument) textComponent.getDocument(), lineEnd);
        Rectangle startRect = textComponent.getUI().modelToView(textComponent, startOffset);
        Rectangle endRect = textComponent.getUI().modelToView(textComponent, endOffset);
        Rectangle visibleRect = new Rectangle(startRect.x - visibleBorder, startRect.y - visibleBorder, 
                                              startRect.x, endRect.y - startRect.y + endRect.height + visibleBorder * 2);
        textComponent.scrollRectToVisible(visibleRect);
       
        //make sure the y coordinate isn't outside the editor bounds otherwise the popup will 'float' beneath the editor
        Rectangle extent = editorUI.getExtentBounds();
        int maxVisibleY = extent.y + extent.height;
        
        Point p = new Point(endRect.x, Math.min(maxVisibleY, endRect.y + endRect.height + 1));
        
        //XXX: The resulting screen coordinates could still be outside the main screen
        SwingUtilities.convertPointToScreen(p, textComponent);
        return p;
    } catch (BadLocationException e) {
        LOG.log(Level.WARNING, "scrollToDifference", e); // NOI18N
    }
    return null;
}
 
Example 10
Source File: DiffSidebar.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private int computeDocumentOffset(int lineOffset) {
    int end = Utilities.getRowStartFromLineOffset(document, lineOffset);
    if (end == -1) {
        Element lineRoot = document.getParagraphElement(0).getParentElement();
        for (end = lineRoot.getElement(lineOffset - 1).getEndOffset(); end > document.getLength(); end--) {
        }
    }
    return end;
}
 
Example 11
Source File: TagBasedFormatter.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private int getInitialIndentFromNextLine(final BaseDocument doc, final int line) throws BadLocationException {
    
    // get initial indent from the next line
    int initialIndent = 0;
    
    int lineStart = Utilities.getRowStartFromLineOffset(doc, line);
    int lineEnd = Utilities.getRowEnd(doc, lineStart);
    int nextNonWhiteLineStart = Utilities.getFirstNonWhiteFwd(doc, lineEnd);
    
    if (nextNonWhiteLineStart > 0){
        initialIndent = Utilities.getRowIndent(doc, nextNonWhiteLineStart, true);
    }
    
    return initialIndent;
}
 
Example 12
Source File: AbstractIndenter.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private boolean doesLineStartWithOurLanguage(BaseDocument doc, int lineIndex, JoinedTokenSequence<T1> joinedTS) throws BadLocationException {
    int rowStartOffset = Utilities.getRowStartFromLineOffset(doc, lineIndex);
    int rowEndOffset = Utilities.getRowEnd(doc, rowStartOffset);
    int firstNonWhite = Utilities.getRowFirstNonWhite(doc, rowStartOffset);
    if (firstNonWhite != -1) {
        // there is something on the line:
        int newRowStartOffset = findLanguageOffset(joinedTS, rowStartOffset, rowEndOffset, true);
        if (newRowStartOffset == -1) {
            // but it is not our langauge
            return false;
        }
    }
    return true;
}
 
Example 13
Source File: CdiGlyphAction.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    Line line = myPart.getLine();
    int startOffset = Utilities.getRowStartFromLineOffset(myDocument, 
            line.getLineNumber());
    myOffset = startOffset+myPart.getColumn();
}
 
Example 14
Source File: TagBasedLexerFormatter.java    From netbeans with Apache License 2.0 4 votes vote down vote up
protected static int getExistingIndent(BaseDocument doc, int line) throws BadLocationException{
    int lineStart = Utilities.getRowStartFromLineOffset(doc, line);
    return IndentUtils.lineIndent(doc, lineStart);
}
 
Example 15
Source File: GotoDialogSupport.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** Perform the goto operation.
 * @return whether the dialog should be made invisible or not
 */
protected boolean performGoto() {
    JTextComponent c = EditorRegistry.lastFocusedComponent();
    if (c != null) {
        try {
            int line = Integer.parseInt(getGotoValueText());

            //issue 188976
            if (line==0)
                line = 1;
            //end of issue 188976
            
            BaseDocument doc = Utilities.getDocument(c);
            if (doc != null) {
                int rowCount = Utilities.getRowCount(doc);
                if (line > rowCount)
                    line = rowCount;
                
                // Obtain the offset where to jump
                int pos = Utilities.getRowStartFromLineOffset(doc, line - 1);
                
                BaseKit kit = Utilities.getKit(c);
                if (kit != null) {
                    Action a = kit.getActionByName(ExtKit.gotoAction);
                    if (a instanceof ExtKit.GotoAction) {
                        pos = ((ExtKit.GotoAction)a).getOffsetFromLine(doc, line - 1);
                    }
                }
                
                if (pos != -1) {
                    Caret caret = c.getCaret();
                    caret.setDot(pos);
                } else {
                    c.getToolkit().beep();
                    return false;
                }
            }
        } catch (NumberFormatException e) {
            c.getToolkit().beep();
            return false;
        }
    }
    return true;
}
 
Example 16
Source File: EmbeddedSectionsHighlighting.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public boolean moveNext() {
    synchronized (EmbeddedSectionsHighlighting.this) {
        if (checkVersion()) {
            if (sequence == null) {
                if(!scanner.isActive()) {
                    return false; //token hierarchy inactive already
                }
                sequence = scanner.tokenSequence();
                sequence.move(startOffset);
            }

            while (sequence.moveNext() && sequence.offset() < endOffset) {
                if (javascripletBackground != null && sequence.token().id() == JspTokenId.SCRIPTLET) {
                    sectionStart = sequence.offset();
                    sectionEnd = sequence.offset() + sequence.token().length();

                    try {
                        int docLen = document.getLength();
                        int startLine = Utilities.getLineOffset((BaseDocument) document, Math.min(sectionStart, docLen));
                        int endLine = Utilities.getLineOffset((BaseDocument) document, Math.min(sectionEnd, docLen));

                        if (startLine != endLine) {
                            // multiline scriplet section
                            // adjust the sections start to the beginning of the firts line
                            int firstLineStartOffset = Utilities.getRowStartFromLineOffset((BaseDocument) document, startLine);
                            if (firstLineStartOffset < sectionStart - 2 &&
                                isWhitespace(document, firstLineStartOffset, sectionStart - 2)) // always preceeded by '<%' hence -2
                            {
                                sectionStart = firstLineStartOffset;
                            }

                            // adjust the sections end to the end of the last line
                            int lines = Utilities.getRowCount((BaseDocument) document);
                            int lastLineEndOffset;
                            if (endLine + 1 < lines) {
                                lastLineEndOffset = Utilities.getRowStartFromLineOffset((BaseDocument) document, endLine + 1);
                            } else {
                                lastLineEndOffset = document.getLength();
                            }
                            
                            if (sectionEnd + 2 >= lastLineEndOffset || // unclosed section
                                isWhitespace(document, sectionEnd + 2, lastLineEndOffset)) // always succeeded by '%>' hence +2
                            {
                                sectionEnd = lastLineEndOffset;
                            }
                        }
                        
                        attributeSet = javascripletBackground;
                        return true;
                        
                    } catch (BadLocationException ble) {
                        LOG.log(Level.WARNING, null, ble);
                    }
                    
                    
                } else if (expressionLanguageBackground != null && sequence.token().id() == JspTokenId.EL) {
                    sectionStart = sequence.offset();
                    sectionEnd = sequence.offset() + sequence.token().length();
                    attributeSet = expressionLanguageBackground;
                    
                    return true;
                }
            }
        }
        
        sectionStart = -1;
        sectionEnd = -1;
        finished = true;

        return false;
    }
}
 
Example 17
Source File: EmbeddedSectionsHighlighting.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public boolean moveNext() {
    synchronized (EmbeddedSectionsHighlighting.this) {
        if (checkVersion()) {
            if (sequence == null) {
                sequence = scanner.tokenSequence();
                if (sequence == null) {
                    return false;
                } else {
                    sequence.move(startOffset);
                }
            }

            int delimiterSize = 0;
            while (sequence.moveNext() && sequence.offset() < endOffset) {
                if (sequence.token().id() == YamlTokenId.DELIMITER) {
                    // opening delimiters can have different lenght
                    delimiterSize = sequence.token().length();
                } else if (YamlTokenId.isRuby(sequence.token().id())) {
                    sectionStart = sequence.offset();
                    sectionEnd = sequence.offset() + sequence.token().length();

                    try {
                        int docLen = document.getLength();
                        int startLine = Utilities.getLineOffset((BaseDocument) document, Math.min(sectionStart, docLen));
                        int endLine = Utilities.getLineOffset((BaseDocument) document, Math.min(sectionEnd, docLen));

                        if (startLine != endLine) {
                            // multiline scriplet section
                            // adjust the sections start to the beginning of the firts line
                            int firstLineStartOffset = Utilities.getRowStartFromLineOffset((BaseDocument) document, startLine);
                            if (firstLineStartOffset < sectionStart - delimiterSize
                                    && isWhitespace(document, firstLineStartOffset, sectionStart - delimiterSize)) // always preceeded by the delimiter
                            {
                                sectionStart = firstLineStartOffset;
                            }

                            // adjust the sections end to the end of the last line
                            int lines = Utilities.getRowCount((BaseDocument) document);
                            int lastLineEndOffset;
                            if (endLine + 1 < lines) {
                                lastLineEndOffset = Utilities.getRowStartFromLineOffset((BaseDocument) document, endLine + 1);
                            } else {
                                lastLineEndOffset = document.getLength() + 1;
                            }

                            if (sectionEnd + 2 >= lastLineEndOffset || // unclosed section
                                    isWhitespace(document, sectionEnd + 2, lastLineEndOffset)) // always succeeded by '%>' hence +2
                            {
                                sectionEnd = lastLineEndOffset;
                            }
                        }
                    } catch (BadLocationException ble) {
                        LOG.log(Level.WARNING, null, ble);
                    }

                    return true;
                }
            }
        }

        sectionStart = -1;
        sectionEnd = -1;
        finished = true;

        return false;
    }
}
 
Example 18
Source File: EmbeddedSectionsHighlighting.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public boolean moveNext() {
    synchronized (EmbeddedSectionsHighlighting.this) {
        if (checkVersion()) {
            if (sequence == null) {
                sequence = scanner.tokenSequence(GspLexerLanguage.getLanguage());

                if (sequence == null) {
                    sectionStart = -1;
                    sectionEnd = -1;
                    finished = true;

                    return false;
                }

                sequence.move(startOffset);
            }

            int delimiterSize = 0;
            while (sequence.moveNext() && sequence.offset() < endOffset) {
                if (sequence.token().id().isDelimiter()) {
                    // opening delimiters can have different lenght
                    delimiterSize = sequence.token().length();
                } else {
                    sectionStart = sequence.offset();
                    sectionEnd = sequence.offset() + sequence.token().length();

                    try {
                        int docLen = document.getLength();
                        int startLine = Utilities.getLineOffset((BaseDocument) document, Math.min(sectionStart, docLen));
                        int endLine = Utilities.getLineOffset((BaseDocument) document, Math.min(sectionEnd, docLen));

                        if (startLine != endLine) {
                            // multiline scriplet section
                            // adjust the sections start to the beginning of the firts line
                            int firstLineStartOffset = Utilities.getRowStartFromLineOffset((BaseDocument) document, startLine);
                            if (firstLineStartOffset < sectionStart - delimiterSize && 
                                isWhitespace(document, firstLineStartOffset, sectionStart - delimiterSize)) // always preceeded by the delimiter
                            {
                                sectionStart = firstLineStartOffset;
                            }

                            // adjust the sections end to the end of the last line
                            int lines = Utilities.getRowCount((BaseDocument) document);
                            int lastLineEndOffset;
                            if (endLine + 1 < lines) {
                                lastLineEndOffset = Utilities.getRowStartFromLineOffset((BaseDocument) document, endLine + 1);
                            } else {
                                lastLineEndOffset = document.getLength() + 1;
                            }
                            
                            if (sectionEnd + 2 >= lastLineEndOffset || // unclosed section
                                isWhitespace(document, sectionEnd + 2, lastLineEndOffset)) // always succeeded by '%>' hence +2
                            {
                                sectionEnd = lastLineEndOffset;
                            }
                        }
                    } catch (BadLocationException ble) {
                        LOG.log(Level.WARNING, null, ble);
                    }
                    
                    return true;
                }
            }
        }
        
        sectionStart = -1;
        sectionEnd = -1;
        finished = true;

        return false;
    }
}
 
Example 19
Source File: CoverageHighlightsContainer.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private Highlights(long version, int startOffset, int endOffset, FileCoverageDetails details) {
    this.version = version;
    this.startOffsetBoundary = startOffset;
    this.endOffsetBoundary = endOffset;

    if (lastPositions == null) {
        positions = new ArrayList<Position>();
        types = new ArrayList<CoverageType>();
        for (int lineno = 0, maxLines = details.getLineCount(); lineno < maxLines; lineno++) {
            CoverageType type = details.getType(lineno);
            if (type == CoverageType.COVERED || type == CoverageType.INFERRED ||
                    type == CoverageType.NOT_COVERED || type == CoverageType.PARTIAL) {
                try {
                    int offset = Utilities.getRowStartFromLineOffset(doc, lineno);
                    if (offset == -1) {
                        continue;
                    }
                    // Attach the highlight position to the beginning of text, such
                    // that if we insert a new line at the beginning of a line (or in
                    // the whitespace region) the highlight will move down with the
                    // text
                    int rowStart = Utilities.getRowFirstNonWhite(doc, offset);
                    if (rowStart != -1) {
                        offset = rowStart;
                    }
                    Position pos = doc.createPosition(offset, Position.Bias.Forward);
                    positions.add(pos);
                    types.add(type);
                } catch (BadLocationException ex) {
                    Exceptions.printStackTrace(ex);
                }
            }
        }
        lastPositions = positions;
        lastTypes = types;
    } else {
        positions = lastPositions;
        types = lastTypes;
    }

    try {
        int lineStart = Utilities.getRowFirstNonWhite(doc, startOffsetBoundary);
        if (lineStart == -1) {
            lineStart = Utilities.getRowStart(doc, startOffsetBoundary);
            index = findPositionIndex(positions, lineStart);
            if (index < 0) {
                index = -index;
            }
        }
    } catch (BadLocationException ble) {
    }
}
 
Example 20
Source File: ExtKit.java    From netbeans with Apache License 2.0 2 votes vote down vote up
/** This method is called by the dialog support
* to translate the line offset to the document position. This
* can be changed for example for the diff operations.
* @param doc document to operate over
* @param lineOffset the line offset to convert to position
* @return document offset that corresponds to the row-start
*  of the line with the line-number equal to (lineOffset + 1).
*/
protected int getOffsetFromLine(BaseDocument doc, int lineOffset) {
    return Utilities.getRowStartFromLineOffset(doc, lineOffset);
}