Java Code Examples for org.netbeans.editor.Utilities#getLineOffset()
The following examples show how to use
org.netbeans.editor.Utilities#getLineOffset() .
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: NbEditorUtilities.java From netbeans with Apache License 2.0 | 6 votes |
/** Get the line object from the given position. * @param doc document for which the line is being retrieved * @param offset position in the document * @param original whether to retrieve the original line (true) before * the modifications were done or the current line (false) * @return the line object * @deprecated Replaced by more generic method having {@link javax.swing.text.Document} parameter. */ public static Line getLine(BaseDocument doc, int offset, boolean original) { DataObject dob = getDataObject(doc); if (dob != null) { LineCookie lc = (LineCookie)dob.getCookie(LineCookie.class); if (lc != null) { Line.Set lineSet = lc.getLineSet(); if (lineSet != null) { try { int lineOffset = Utilities.getLineOffset(doc, offset); return original ? lineSet.getOriginal(lineOffset) : lineSet.getCurrent(lineOffset); } catch (BadLocationException e) { } } } } return null; }
Example 2
Source File: MethodChooser.java From netbeans with Apache License 2.0 | 6 votes |
@Override public void mouseClicked(MouseEvent e) { if (e.isPopupTrigger()) { return; } e.consume(); int position = editorPane.viewToModel(e.getPoint()); if (e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON1) { if (position < 0) { return; } if (mousedIndex != -1) { selectedIndex = mousedIndex; releaseUI(true); return; } } try { int line = Utilities.getLineOffset((BaseDocument) doc, position) + 1; if (line < startLine || line > endLine) { releaseUI(false); return; } } catch (BadLocationException ex) { } }
Example 3
Source File: IndentCasesTest.java From netbeans with Apache License 2.0 | 6 votes |
private void test(String fileName, int lineNum, int offset, int endLineNum, int endOffset) throws Exception { EditorOperator.closeDiscardAll(); EditorOperator op = openFile(fileName); op.setCaretPositionToLine(lineNum); op.setCaretPositionRelative(offset - 1); if (debug) { Thread.sleep(3000); // to be visible ;-) } op.pressKey(KeyEvent.VK_ENTER); op.waitModified(true); int newPossition = op.txtEditorPane().getCaretPosition(); int newLine = Utilities.getLineOffset(doc, newPossition) + 1; int newOffset = newPossition - Utilities.getRowStart(doc, newPossition); if (debug) { Thread.sleep(3000); // to be visible ;-) } assertEquals("FINAL POSSITION", endLineNum, newLine); assertEquals("FINAL POSSITION", endOffset - 1, newOffset); }
Example 4
Source File: TagAlignmentTest.java From netbeans with Apache License 2.0 | 6 votes |
private void test(String fileName, int lineNum, int offset, String text, int endLineNum, int endOffset) throws Exception { EditorOperator.closeDiscardAll(); EditorOperator op = openFile(fileName); op.setCaretPositionToLine(lineNum); op.setCaretPositionRelative(offset - 1); if (debug) { Thread.sleep(3000); // to be visible ;-) } for (char c : text.toCharArray()) { op.typeKey(c); } CompletionTest.waitTypingFinished(doc); int newPossition = op.txtEditorPane().getCaretPosition(); int newLine = Utilities.getLineOffset(doc, newPossition) + 1; int newOffset = newPossition - Utilities.getRowStart(doc, newPossition); if (debug) { Thread.sleep(3000); // to be visible ;-) } assertEquals("FINAL POSSITION", endLineNum, newLine); assertEquals("FINAL POSSITION", endOffset - 1, newOffset); }
Example 5
Source File: NbEditorDocument.java From netbeans with Apache License 2.0 | 6 votes |
public int getLine() { int offset = pos.getOffset(); if (lastKnownOffset != -1 && lastKnownLine != -1) { if (lastKnownOffset == offset) { return lastKnownLine; } } try { lastKnownLine = Utilities.getLineOffset(doc, offset); lastKnownOffset = offset; } catch (BadLocationException e) { lastKnownOffset = -1; lastKnownLine = 0; } return lastKnownLine; }
Example 6
Source File: TagBasedFormatter.java From netbeans with Apache License 2.0 | 6 votes |
protected int getIndentForTagParameter(BaseDocument doc, TokenItem tag) throws BadLocationException{ int tagStartLine = Utilities.getLineOffset(doc, tag.getOffset()); TokenItem currentToken = tag.getNext(); /* * Find the offset of the first attribute if it is specified on the same line as the opening of the tag * e.g. <tag |attr= * */ while (currentToken != null && isWSTag(currentToken) && tagStartLine == Utilities.getLineOffset(doc, currentToken.getOffset())){ currentToken = currentToken.getNext(); } if (tag != null && !isWSTag(currentToken) && tagStartLine == Utilities.getLineOffset(doc, currentToken.getOffset())){ return currentToken.getOffset() - Utilities.getRowIndent(doc, currentToken.getOffset()) - Utilities.getRowStart(doc, currentToken.getOffset()); } return getShiftWidth(); // default; }
Example 7
Source File: DiffSidebar.java From netbeans with Apache License 2.0 | 6 votes |
private int getLineFromMouseEvent(MouseEvent e){ int line = -1; EditorUI editorUI = Utilities.getEditorUI(textComponent); if (editorUI != null) { try{ JTextComponent component = editorUI.getComponent(); if (component != null) { TextUI textUI = component.getUI(); int clickOffset = textUI.viewToModel(component, new Point(0, e.getY())); line = Utilities.getLineOffset(document, clickOffset); } }catch (BadLocationException ble){ LOG.log(Level.WARNING, "getLineFromMouseEvent", ble); // NOI18N } } return line; }
Example 8
Source File: GlobalIsNotDefined.java From netbeans with Apache License 2.0 | 5 votes |
private void addHint(JsRuleContext context, List<Hint> hints, int offset, String name, OffsetRange range) throws BadLocationException { boolean add = false; Document document = context.getJsParserResult().getSnapshot().getSource().getDocument(false); if (offset > -1) { if (document != null && document instanceof BaseDocument) { BaseDocument baseDocument = (BaseDocument)document; int lineOffset = Utilities.getLineOffset(baseDocument, offset); int lineOffsetRange = Utilities.getLineOffset(baseDocument, range.getStart()); add = lineOffset == lineOffsetRange; } } else { add = true; if (document != null) { ((AbstractDocument)document).readLock(); try { TokenSequence ts = LexerUtils.getTokenSequence(document, range.getStart(), JsTokenId.javascriptLanguage(), true); ts.move(range.getStart()); if (ts.moveNext()) { add = ts.token().id() != JsTokenId.DOC_COMMENT; } } finally { ((AbstractDocument) document).readUnlock(); } } } if (add) { List<HintFix> fixes; fixes = new ArrayList<HintFix>(); fixes.add(new AddJsHintFix(context.getJsParserResult().getSnapshot(), offset, name)); hints.add(new Hint(this, Bundle.JsGlobalIsNotDefinedHintDesc(name), context.getJsParserResult().getSnapshot().getSource().getFileObject(), ModelUtils.documentOffsetRange(context.getJsParserResult(), range.getStart(), range.getEnd()), fixes, 500)); } }
Example 9
Source File: AnnotationBar.java From netbeans with Apache License 2.0 | 5 votes |
/** GlyphGutter copy pasted utility method. */ private int getLineFromMouseEvent(MouseEvent e){ int line = -1; if (editorUI != null) { try{ JTextComponent component = editorUI.getComponent(); BaseTextUI textUI = (BaseTextUI)component.getUI(); int clickOffset = textUI.viewToModel(component, new Point(0, e.getY())); line = Utilities.getLineOffset(doc, clickOffset); }catch (BadLocationException ble){ } } return line; }
Example 10
Source File: AnnotationBar.java From netbeans with Apache License 2.0 | 5 votes |
/** GlyphGutter copy pasted utility method. */ private int getLineFromMouseEvent(MouseEvent e){ int line = -1; if (editorUI != null) { try{ JTextComponent component = editorUI.getComponent(); BaseTextUI textUI = (BaseTextUI)component.getUI(); int clickOffset = textUI.viewToModel(component, new Point(0, e.getY())); line = Utilities.getLineOffset(doc, clickOffset); }catch (BadLocationException ble){ } } return line; }
Example 11
Source File: TagBasedLexerFormatter.java From netbeans with Apache License 2.0 | 5 votes |
protected int getIndentForTagParameter(BaseDocument doc, JoinedTokenSequence tokenSequence, int tagOffset) throws BadLocationException { int originalOffset = tokenSequence.offset(); int tagStartLine = Utilities.getLineOffset(doc, tagOffset); tokenSequence.move(tagOffset); Token<?> token; int tokenOffset; boolean thereWasWS = false; int shift = doc.getShiftWidth(); // default; /* * Find the offset of the first attribute if it is specified on the same line as the opening of the tag * e.g. <tag |attr= * */ while (tokenSequence.moveNext()) { token = tokenSequence.token(); tokenOffset = tokenSequence.offset(); boolean isWSToken = isWSToken(token); if (thereWasWS && (!isWSToken || tagStartLine != Utilities.getLineOffset(doc, tokenOffset))) { if (!isWSToken && tagStartLine == Utilities.getLineOffset(doc, tokenOffset)) { shift = tokenOffset - Utilities.getRowIndent(doc, tokenOffset) - Utilities.getRowStart(doc, tokenOffset); } break; } else if (isWSToken){ thereWasWS = true; } } tokenSequence.move(originalOffset); tokenSequence.moveNext(); return shift; }
Example 12
Source File: AnnotationTestUtilities.java From netbeans with Apache License 2.0 | 5 votes |
public int getLine() { try { return Utilities.getLineOffset(doc, getOffset()); } catch (BadLocationException e) { IllegalStateException exc = new IllegalStateException(); exc.initCause(e); throw exc; } }
Example 13
Source File: AnnotationTestUtilities.java From netbeans with Apache License 2.0 | 5 votes |
public int getLine() { try { return Utilities.getLineOffset(doc, getOffset()); } catch (BadLocationException e) { IllegalStateException exc = new IllegalStateException(); exc.initCause(e); throw exc; } }
Example 14
Source File: AnnotationViewDataImplTest.java From netbeans with Apache License 2.0 | 5 votes |
public int getLine() { try { return Utilities.getLineOffset(doc, getOffset()); } catch (BadLocationException e) { IllegalStateException exc = new IllegalStateException(); exc.initCause(e); throw exc; } }
Example 15
Source File: AnnotationBar.java From netbeans with Apache License 2.0 | 5 votes |
/** GlyphGutter copy pasted utility method. */ private int getLineFromMouseEvent(MouseEvent e){ int line = -1; if (editorUI != null) { try{ JTextComponent component = editorUI.getComponent(); BaseTextUI textUI = (BaseTextUI)component.getUI(); int clickOffset = textUI.viewToModel(component, new Point(0, e.getY())); line = Utilities.getLineOffset(doc, clickOffset); }catch (BadLocationException ble){ } } return line; }
Example 16
Source File: TagBasedLexerFormatter.java From netbeans with Apache License 2.0 | 4 votes |
protected static int getNumberOfLines(BaseDocument doc) throws BadLocationException { return Utilities.getLineOffset(doc, doc.getLength()) + 1; }
Example 17
Source File: FoldView.java From netbeans with Apache License 2.0 | 4 votes |
@NbBundle.Messages({ "# {0} - number of lines of folded code", "FMT_contentSummary={0} {0,choice,0#lines|1#line|1<lines}" }) private String resolvePlaceholder(String text, int at) { if ((options & 3) == 0) { return text; } Document d = getDocument(); if (!(d instanceof BaseDocument)) { return null; } BaseDocument bd = (BaseDocument)d; CharSequence contentSeq = ""; // NOI18N String summary = ""; // NOI18N int mask = options; try { if ((options & 1) > 0) { contentSeq = FoldContentReaders.get().readContent( org.netbeans.lib.editor.util.swing.DocumentUtilities.getMimeType(textComponent), d, fold, fold.getType().getTemplate()); if (contentSeq == null) { mask &= ~1; } } if ((options & 2) > 0) { int start = fold.getStartOffset(); int end = fold.getEndOffset(); int startLine = Utilities.getLineOffset(bd, start); int endLine = Utilities.getLineOffset(bd, end) + 1; if (endLine <= startLine + 1) { mask &= ~2; } else { summary = FMT_contentSummary((endLine - startLine)); } } } catch (BadLocationException ex) { } if (mask == 0) { return text; } String replacement = NbBundle.getMessage(FoldView.class, "FMT_ContentPlaceholder_" + (mask & 3), contentSeq, summary); // NOI18N StringBuilder sb = new StringBuilder(text.length() + replacement.length()); sb.append(text.subSequence(0, at)); sb.append(replacement); sb.append(text.subSequence(at + FoldTemplate.CONTENT_PLACEHOLDER.length(), text.length())); return sb.toString(); }
Example 18
Source File: HintsUI.java From netbeans with Apache License 2.0 | 4 votes |
boolean invokeDefaultAction(boolean onlyActive) { JTextComponent comp = getComponent(); if (comp == null) { Logger.getLogger(HintsUI.class.getName()).log(Level.WARNING, "HintsUI.invokeDefaultAction called, but comp == null"); return false; } Document doc = comp.getDocument(); cancel.set(false); if (doc instanceof BaseDocument) { try { Rectangle carretRectangle = comp.modelToView(comp.getCaretPosition()); int line = Utilities.getLineOffset((BaseDocument) doc, comp.getCaretPosition()); FixData fixes; String description; if (!onlyActive) { refresh(doc, comp.getCaretPosition()); AnnotationHolder holder = getAnnotationHolder(doc); Pair<FixData, String> fixData = holder != null ? holder.buildUpFixDataForLine(line) : null; if (fixData == null) return false; fixes = fixData.first(); description = fixData.second(); } else { AnnotationDesc activeAnnotation = ((BaseDocument) doc).getAnnotations().getActiveAnnotation(line); if (activeAnnotation == null) { return false; } String type = activeAnnotation.getAnnotationType(); if (!FixAction.getFixableAnnotationTypes().contains(type) && onlyActive) { return false; } if (onlyActive) { refresh(doc, comp.getCaretPosition()); } Annotations annotations = ((BaseDocument) doc).getAnnotations(); AnnotationDesc desc = annotations.getAnnotation(line, type); ParseErrorAnnotation annotation = null; if (desc != null) { annotations.frontAnnotation(desc); annotation = findAnnotation(doc, desc, line); } if (annotation == null) { return false; } fixes = annotation.getFixes(); description = annotation.getDescription(); } Point p = comp.modelToView(Utilities.getRowStartFromLineOffset((BaseDocument) doc, line)).getLocation(); p.y += carretRectangle.height; if(comp.getParent() instanceof JViewport) { p.x += ((JViewport)comp.getParent()).getViewPosition().x; } if(comp.getParent() instanceof JLayeredPane && comp.getParent().getParent() instanceof JViewport) { p.x += ((JViewport)comp.getParent().getParent()).getViewPosition().x; } showPopup(fixes, description, comp, p); return true; } catch (BadLocationException ex) { ErrorManager.getDefault().notify(ex); } } return false; }
Example 19
Source File: EmbeddedSectionsHighlighting.java From netbeans with Apache License 2.0 | 4 votes |
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 20
Source File: ErrorAnnotationImpl.java From netbeans with Apache License 2.0 | 4 votes |
/** Transforms ErrosInfo to Annotation */ private Collection getAnnotations(ErrorInfo[] errors, StyledDocument document) { BaseDocument doc = (BaseDocument) document; HashMap map = new HashMap(errors.length); for (int i = 0; i < errors.length; i ++) { ErrorInfo err = errors[i]; int line = err.getLine(); int column = err.getColumn(); if (line<0){ // place error annotation on the 1st non-empty line try { int firstNonWS = Utilities.getFirstNonWhiteFwd(doc, 0); line = Utilities.getLineOffset(doc, firstNonWS) + 1; } catch (BadLocationException ex) { Exceptions.printStackTrace(ex); } } if (column < 0){ column = 0; } String message = err.getDescription(); LineSetAnnotation ann; switch (err.getType()){ case ErrorInfo.JSP_ERROR: ann = new JspParserErrorAnnotation(line, column, message, (NbEditorDocument)document); break; default: ann = new JspParserErrorAnnotation(line, column, message, (NbEditorDocument)document); break; } // This is trying to ensure that annotations on the same // line are "chained" (so we get a single annotation for // multiple errors on a line). // If we knew the errors were sorted by file & line number, // this would be easy (and we wouldn't need to do the hashmap // "sort" Integer lineInt = new Integer(line); /*LineSetAnnotation prev = (LineSetAnnotation)map.get(lineInt); if (prev != null) { prev.chain(ann); } else if (map.size() < maxErrors) {*/ map.put(lineInt, ann); //} } return map.values(); }