/** * converts (line, col) into offset. Line and column info are 1-based, so * the start of document is (1,1) */ public static int getDocumentOffset(BaseDocument doc, int lineIndex, int colIndex) { return Utilities.getRowStartFromLineOffset(doc, lineIndex - 1) + (colIndex - 1); }
/** 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); }
/** * Find offset in text for given line and column * Never returns negative number */ public static int getOffset(BaseDocument doc, int lineNumber, int columnNumber) { assert lineNumber > 0 : "Line number must be at least 1 and was: " + lineNumber; assert columnNumber > 0 : "Column number must be at least 1 ans was: " + columnNumber; int offset = Utilities.getRowStartFromLineOffset(doc, lineNumber - 1); offset += (columnNumber - 1); // some sanity checks if (offset < 0){ offset = 0; } return offset; }
/** * 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); }
/** Notify view that it is necessary to redraw the line of the document */ protected void refreshLine(int line) { fireChangedLine(line); int start = Utilities.getRowStartFromLineOffset(doc, line); int end = Utilities.getRowStartFromLineOffset(doc, line+1); if (end == -1) end = doc.getLength(); doc.repaintBlock(start, end); }
/** Notify view that it is necessary to redraw the line of the document */ protected void refreshLine(int line) { fireChangedLine(line); int start = Utilities.getRowStartFromLineOffset(doc, line); int end = Utilities.getRowStartFromLineOffset(doc, line + 1); if (end == -1) end = doc.getLength(); doc.repaintBlock(start, end); }
private static int lineToPosition(JEditorPane pane, int docLine) { Document doc = pane.getDocument(); int lineSt = 0; if (doc instanceof BaseDocument) { // use NB utilities for NB documents lineSt = Utilities.getRowStartFromLineOffset((BaseDocument) doc, docLine); } else { // not NB document, count lines int len = doc.getLength(); try { String text = doc.getText(0, len); boolean afterEOL = false; for (int i = 0; i < len; i++) { char c = text.charAt(i); if (c == '\n') { docLine--; if (docLine == 0) { return lineSt; } afterEOL = true; } else if (afterEOL) { lineSt = i; afterEOL = false; } } } catch (BadLocationException e) { } } return lineSt; }
private int countLinesBetweenLineOffsets(int startLineOffset, int endLineOffset) throws BadLocationException { int result = 0; for (int lineOffset = startLineOffset; lineOffset < endLineOffset; lineOffset++) { int rowStartFromLineOffset = Utilities.getRowStartFromLineOffset(baseDocument, lineOffset); if (!Utilities.isRowWhite(baseDocument, rowStartFromLineOffset) && !isJustCommentOnLine(rowStartFromLineOffset)) { result++; } } return result; }
protected int removeLine(final Document doc, final int line) throws BadLocationException { final int i = Utilities.getRowStartFromLineOffset((BaseDocument)doc, line - 1); doc.remove(i, Utilities.getRowEnd((BaseDocument)doc, i) - i); return i; }
@Override public void run() { Line line = myPart.getLine(); int startOffset = Utilities.getRowStartFromLineOffset(myDocument, line.getLineNumber()); myOffset = startOffset+myPart.getColumn(); }
public void run() { try { final BaseDocument doc = (BaseDocument)txt.getDocument(); final int s = Utilities.getRowStartFromLineOffset(doc, getSelectionStartLine(txt) - 1); final int e = Utilities.getRowEnd(txt, Utilities.getRowStartFromLineOffset(doc, getSelectionEndLine(txt) - 1)); doc.insertString(e, "\n//#enddebug", null); //NOI18N doc.insertString(s, "//#mdebug\n", null); //NOI18N txt.setSelectionEnd(txt.getSelectionStart()); } catch (BadLocationException ble) { ErrorManager.getDefault().notify(ble); } RecommentAction.actionPerformed(txt); } });
@Override public void run() { try { int line = Utilities.getLineOffset((BaseDocument) doc, currentPosition); int startOffset = Utilities.getRowStartFromLineOffset((BaseDocument) doc, line); int endOffset = Utilities.getRowEnd((BaseDocument) doc, startOffset); AnnotationDesc desc = annotations.getActiveAnnotation(line); if (desc == null) { return ; } Collection<BaseAnnotation> annos; if (COMBINED_TYPES.contains(desc.getAnnotationType())) { annos = findAnnotations(comp, startOffset, endOffset); } else { annos = Collections.singletonList(findAnnotation(comp, desc, startOffset, endOffset)); } for (BaseAnnotation a : annos) { if (a != null) { anno.set(a); point.set(comp.modelToView(startOffset).getLocation()); break; } } } catch (BadLocationException ex) { ex.printStackTrace(); } } });
public void run() { try { final BaseDocument doc = (BaseDocument)txt.getDocument(); final int s = Utilities.getRowStartFromLineOffset(doc, getSelectionStartLine(txt) - 1); final int e = Utilities.getRowEnd(txt, Utilities.getRowStartFromLineOffset(doc, getSelectionEndLine(txt) - 1)); final String text = doc.getText(s, e-s); doc.insertString(e, "\n//#else\n" + text + "\n//#endif", null); //NOI18N doc.insertString(s, "//#if \n", null); //NOI18N txt.setSelectionStart(s + 6); txt.setSelectionEnd(s + 6); } catch (BadLocationException ble) { ErrorManager.getDefault().notify(ble); } RecommentAction.actionPerformed(txt); } });
int packageLine = getPackageLineIndex(doc); int afterPackageLine = packageLine + 1; int afterPackageOffset = Utilities.getRowStartFromLineOffset(doc, afterPackageLine); int importLine = getAppropriateLine(doc, fqName); int importOffset = Utilities.getRowStartFromLineOffset(doc, importLine); edits.replace(importOffset, 0, "import " + fqName + "\n", false, 0); int afterImportsOffset = Utilities.getRowStartFromLineOffset(doc, importLine);
private static void jumpToLine(JEditorPane pane, SourceFileInfo sourceFileInfo) { assert SwingUtilities.isEventDispatchThread() : "must be called in EDT"; long start; if (sourceFileInfo.hasOffset()) { start = sourceFileInfo.getOffset(); } else { start = Utilities.getRowStartFromLineOffset((BaseDocument) pane.getDocument(), sourceFileInfo.getLine() - 1); } if (pane.getDocument() != null && start >= 0 && start < pane.getDocument().getLength()) { pane.setCaretPosition((int) start); } StatusDisplayer.getDefault().setStatusText(""); // NOI18N } }
/***** Annotation Stuff ********/ static void processAnnotations(NbEditorDocument doc, List<PPLine> lineList) { //XXX needs to be split for errors and warnings final ArrayList<ErrorDescription> errs = new ArrayList(); DataObject dob = NbEditorUtilities.getDataObject(doc); FileObject fo = dob == null ? null : dob.getPrimaryFile(); for (PPLine line : lineList ) { for (PPLine.Error err : line.getErrors()) { PPToken tok = err.token; int shift = (tok.getType() == LineParserTokens.END_OF_FILE || tok.getType() == LineParserTokens.END_OF_LINE || tok.getType() == LineParserTokens.OTHER_TEXT) ? Math.max(1, tok.getPadding().length()) : 0; int loff = NbDocument.findLineOffset(doc, line.getLineNumber()-1); errs.add(ErrorDescriptionFactory.createErrorDescription(err.warning ? Severity.WARNING : Severity.ERROR, err.message, fo, loff + tok.getColumn() - shift, loff + tok.getColumn() + tok.getText().length())); } ArrayList<Fix> fixes = new ArrayList(); int start = Utilities.getRowStartFromLineOffset(doc, line.getLineNumber()-1); if (line.getTokens().size() > 1 && "//#include".equals(line.getTokens().get(0).getText())) { //NOI18N fixes.add(new InlineIncludeHint((NbEditorDocument) doc,start, line.getTokens().get(1).getText())); } else if (line.getType() == PPLine.OLDIF || line.getType() == PPLine.OLDENDIF) { PPBlockInfo b = line.getBlock(); while (b != null && b.getType() != PPLine.OLDIF) { b = b.getParent(); } if (b != null) fixes.add(new ReplaceOldSyntaxHint(doc, lineList, b)); } if (line.getType() == PPLine.UNKNOWN) fixes.add(new DisableHint((NbEditorDocument) doc,start)); if (fixes.size() > 0) errs.add(ErrorDescriptionFactory.createErrorDescription(Severity.HINT, NbBundle.getMessage(DocumentPreprocessor.class, "LBL_PreprocessorHint"), fixes, doc, line.getLineNumber())); //NOI18N } HintsController.setErrors(doc, "preprocessor-errors", errs); //NOI18N }
private static void jumpToElement(JEditorPane pane, PointOrOffsetable pointOrOffsetable) { //start = jumpLineStart ? lineToPosition(pane, element.getStartPosition().getLine()-1) : element.getStartOffset(); int start; Offsetable element = pointOrOffsetable.getOffsetable(); Point point = pointOrOffsetable.getPoint(); if (element == null) { start = Utilities.getRowStartFromLineOffset((BaseDocument) pane.getDocument(), point.line - 1); start += point.column; } else { start = element.getOffset(); } if (pane.getDocument() != null && start >= 0 && start < pane.getDocument().getLength()) { pane.setCaretPosition(start); if (DEBUG) { String traceName; System.err.println("I'm going to " + start + " for element " + pointOrOffsetable); } } StatusDisplayer.getDefault().setStatusText(""); // NOI18N } // NB document independent method
if (doc != null) { int pos = Utilities.getRowStartFromLineOffset(doc, line - 1);
offset = annos.getActiveAnnotation(line).getOffset(); else offset = Utilities.getRowStartFromLineOffset(doc, line); if (editorUI.getComponent().getCaret().getDot() != offset) JumpList.checkAddEntry();
public void run() { try { if (b.getType() == PPLine.ELSE) { //insert new block before else final int offs = Utilities.getRowStartFromLineOffset(doc, b.getStartLine() - 1); doc.insertString(offs, "//#elif \n" + text + "\n", null); //NOI18N txt.setSelectionStart(offs + 8); txt.setSelectionEnd(offs + 8); } else { //insert new block after the current final int offs = Utilities.getRowEnd(txt, Utilities.getRowStartFromLineOffset(doc, b.getEndLine() - (b.hasFooter() ? 2 : 1))); doc.insertString(offs, "\n//#elif \n" + text, null); //NOI18N txt.setSelectionStart(offs + 9); txt.setSelectionEnd(offs + 9); } } catch (BadLocationException ble) { ErrorManager.getDefault().notify(ble); } RecommentAction.actionPerformed(txt); } });