Java Code Examples for org.openide.text.CloneableEditorSupport#createPositionRef()

The following examples show how to use org.openide.text.CloneableEditorSupport#createPositionRef() . 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: RelationshipMappingRename.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
 public PositionBounds getPosition() {
    try {
        DataObject dobj = DataObject.find(getParentFile());
        if (dobj != null) {
            EditorCookie.Observable obs = (EditorCookie.Observable)dobj.getLookup().lookup(EditorCookie.Observable.class);
            if (obs != null && obs instanceof CloneableEditorSupport) {
                CloneableEditorSupport supp = (CloneableEditorSupport)obs;

            PositionBounds bounds = new PositionBounds(
                    supp.createPositionRef(loc[0], Position.Bias.Forward),
                    supp.createPositionRef(Math.max(loc[0], loc[1]), Position.Bias.Forward)
                    );

            return bounds;
        }
        }
    } catch (DataObjectNotFoundException ex) {
        LOG.log(Level.INFO, "Can't resolve", ex);//NOI18N
    }
    return null;
}
 
Example 2
Source File: RelationshipMappingWhereUsed.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
 public PositionBounds getPosition() {
    try {
        DataObject dobj = DataObject.find(getParentFile());
        if (dobj != null) {
            EditorCookie.Observable obs = (EditorCookie.Observable)dobj.getLookup().lookup(EditorCookie.Observable.class);
            if (obs != null && obs instanceof CloneableEditorSupport) {
                CloneableEditorSupport supp = (CloneableEditorSupport)obs;

            PositionBounds bounds = new PositionBounds(
                    supp.createPositionRef(loc[0], Position.Bias.Forward),
                    supp.createPositionRef(Math.max(loc[0], loc[1]), Position.Bias.Forward)
                    );

            return bounds;
        }
        }
    } catch (DataObjectNotFoundException ex) {
        LOG.log(Level.INFO, "Can't resolve", ex);//NOI18N
    }
    return null;
}
 
Example 3
Source File: Utilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static List<PositionBounds> prepareSpansFor(FileObject file, Iterable<? extends int[]> spans) {
    List<PositionBounds> result = new ArrayList<PositionBounds>();

    try {
        DataObject d = DataObject.find(file);
        EditorCookie ec = d.getLookup().lookup(EditorCookie.class);
        CloneableEditorSupport ces = (CloneableEditorSupport) ec;

        result = new LinkedList<PositionBounds>();

        for (int[] span : spans) {
            PositionRef start = ces.createPositionRef(span[0], Bias.Forward);
            PositionRef end = ces.createPositionRef(span[1], Bias.Forward);

            result.add(new PositionBounds(start, end));
        }
    } catch (DataObjectNotFoundException ex) {
        Exceptions.printStackTrace(ex);
    }

    return result;
}
 
Example 4
Source File: AbstractRefactoringElement.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public PositionBounds getPosition() {
    try {
        DataObject dobj = DataObject.find(getParentFile());
        if (dobj != null) {
            EditorCookie.Observable obs = (EditorCookie.Observable)dobj.getCookie(EditorCookie.Observable.class);
            if (obs != null && obs instanceof CloneableEditorSupport) {
                CloneableEditorSupport supp = (CloneableEditorSupport)obs;

                if (loc == null) {
                    loc = location();
                }
            PositionBounds bounds = new PositionBounds(
                    supp.createPositionRef(loc[0], Position.Bias.Forward),
                    supp.createPositionRef(Math.max(loc[0], loc[1]), Position.Bias.Forward)
                    );
            
            return bounds;
        }
        }
    } catch (DataObjectNotFoundException ex) {
        ex.printStackTrace();
    }
    return null;
}
 
Example 5
Source File: ModificationsTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testInputStream() throws Exception {
    File file = new File(getDataDir(), "faces-config1.xml");
    FileObject fileObject = FileUtil.toFileObject(file);
    CloneableEditorSupport editor
                        = JSFEditorUtilities.findCloneableEditorSupport(DataObject.find(fileObject));
    Modifications modifications = new Modifications();
    Modifications.Difference difference = new Modifications.Difference(
            Modifications.Difference.Kind.CHANGE, 
            editor.createPositionRef(549, Bias.Forward), 
            editor.createPositionRef(549+21, Bias.Backward), 
            "org.ncl.backing.Login", "org.ncl.forward.Login", "");
    File outFile = new File(getWorkDir(), "faces-modification.xml");
    FileWriter fWriter = new FileWriter(outFile);
    modifications.addDifference(fileObject, difference);
    List<Difference> differences = new LinkedList();
    differences.add(difference);
    modifications.commit(fileObject, differences, fWriter);
    fWriter.close();
    assertFile(outFile, getGoldenFile("gold-modifications.xml"));
   
    
}
 
Example 6
Source File: HintsControllerImpl.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static PositionBounds linePart(FileObject file, int start, int end) {
    try {
        DataObject od = DataObject.find(file);
        
        if (od == null)
            return null;
        
        EditorCookie ec = od.getCookie(EditorCookie.class);
        
        if (!(ec instanceof CloneableEditorSupport)) {
            return null;
        }
        
        final CloneableEditorSupport ces = (CloneableEditorSupport) ec;
        
        checkOffsetsAndLog(start, end);
        
        return new PositionBounds(ces.createPositionRef(start, Position.Bias.Forward), ces.createPositionRef(end, Position.Bias.Backward));
    } catch (IOException e) {
        LOG.log(Level.INFO, null, e);
        return null;
    }
}
 
Example 7
Source File: CPRenameRefactoringPlugin.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void refactorElements(ModificationResult modificationResult, RefactoringElementContext context, Collection<RefactoringElement> elementsToRename, String renameMsg) throws IOException, ParseException {
    Map<FileObject, List<Difference>> file2diffs = new HashMap<>();
    for (RefactoringElement re : elementsToRename) {
        CloneableEditorSupport editor = GsfUtilities.findCloneableEditorSupport(re.getFile());

        Difference diff = new Difference(Difference.Kind.CHANGE,
                editor.createPositionRef(re.getRange().getStart(), Bias.Forward),
                editor.createPositionRef(re.getRange().getEnd(), Bias.Backward),
                re.getName(),
                refactoring.getNewName(),
                renameMsg);

        List<Difference> diffs = file2diffs.get(re.getFile());
        if (diffs == null) {
            diffs = new ArrayList<>();
            file2diffs.put(re.getFile(), diffs);
        }
        diffs.add(diff);
    }

    for (Entry<FileObject, List<Difference>> entry : file2diffs.entrySet()) {
        modificationResult.addDifferences(entry.getKey(), entry.getValue());
    }
}
 
Example 8
Source File: WhereUsedElement.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static WhereUsedElement create(int start, int end, CompilationInfo compiler, boolean inTest, boolean inPlatform, boolean inDependency) {
    CharSequence content = compiler.getSnapshot().getText();
    LineMap lm = compiler.getCompilationUnit().getLineMap();
    long line = lm.getLineNumber(start);
    long endLine = lm.getLineNumber(end);
    long sta = lm.getStartPosition(line);
    int eof = content.length();
    long lastLine = lm.getLineNumber(eof);
    long en = lastLine > endLine ? lm.getStartPosition(endLine + 1) - 1 : eof;
    StringBuilder sb = new StringBuilder();
    sb.append(UIUtilities.getHtml(trimStart(content.subSequence((int) sta, start).toString())));
    sb.append("<b>"); //NOI18N
    sb.append(content.subSequence(start, end));
    sb.append("</b>");//NOI18N
    sb.append(UIUtilities.getHtml(trimEnd(content.subSequence(end, (int) en).toString())));
    
    DataObject dob = null;
    try {
        dob = DataObject.find(compiler.getFileObject());
    } catch (DataObjectNotFoundException ex) {
        Exceptions.printStackTrace(ex);
    }
    CloneableEditorSupport ces = JavaWhereUsedQueryPlugin.findCloneableEditorSupport(dob);
    PositionRef ref1 = ces.createPositionRef(start, Bias.Forward);
    PositionRef ref2 = ces.createPositionRef(end, Bias.Forward);
    PositionBounds bounds = new PositionBounds(ref1, ref2);
    return new WhereUsedElement(bounds, sb.toString().trim(),
            content.subSequence((int)sta, (int)en).toString(),
            compiler.getFileObject(), null, compiler, null, inTest, inPlatform, inDependency, true, false);
}
 
Example 9
Source File: FindUsagesElement.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public PositionBounds getPosition() {
    OffsetRange range = ASTUtils.getRange(usageElement.getNode(), doc);
    if (range == OffsetRange.NONE) {
        return null;
    }

    CloneableEditorSupport ces = GroovyProjectUtil.findCloneableEditorSupport(usageElement.getFileObject());
    PositionRef ref1 = ces.createPositionRef(range.getStart(), Position.Bias.Forward);
    PositionRef ref2 = ces.createPositionRef(range.getEnd(), Position.Bias.Forward);
    return new PositionBounds(ref1, ref2);
}
 
Example 10
Source File: RefactoringUtil.java    From netbeans with Apache License 2.0 5 votes vote down vote up
static PositionRef[] getPostionRefs(ELElement elem, Node targetNode) {
    OffsetRange nodeRange = elem.getOriginalOffset(targetNode);

    CloneableEditorSupport editor = GsfUtilities.findCloneableEditorSupport(elem.getSnapshot().getSource().getFileObject());
    if (editor != null) {
        PositionRef start = editor.createPositionRef(nodeRange.getStart(), Bias.Forward);
        PositionRef end = editor.createPositionRef(nodeRange.getEnd(), Bias.Backward);
        return new PositionRef[]{start, end};
    }
    return new PositionRef[0];
}
 
Example 11
Source File: WarningFileElement.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public PositionBounds getPosition() {
    if (bounds == null) {
        CloneableEditorSupport ces = RefactoringUtils.findCloneableEditorSupport(file);
        PositionRef ref1 = ces.createPositionRef(0, Bias.Forward);
        PositionRef ref2 = ces.createPositionRef(0, Bias.Forward);
        bounds = new PositionBounds(ref1, ref2);
    }
    return bounds;
}
 
Example 12
Source File: WhereUsedElement.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static WhereUsedElement create(CloneableEditorSupport ces, FileObject fo, String name, String html, OffsetRange range, Icon icon) {
    int start = range.getStart();
    int end = range.getEnd();

    PositionRef ref1 = ces.createPositionRef(start, Bias.Forward);
    PositionRef ref2 = ces.createPositionRef(end, Bias.Forward);
    PositionBounds bounds = new PositionBounds(ref1, ref2);

    return new WhereUsedElement(bounds, html,
           fo, name, new OffsetRange(start, end), icon);
}
 
Example 13
Source File: WhereUsedElement.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static WhereUsedElement create(CloneableEditorSupport ces, FileObject fo, String name, String html, OffsetRange range, Icon icon) {
    int start = range.getStart();
    int end = range.getEnd();

    PositionRef ref1 = ces.createPositionRef(start, Bias.Forward);
    PositionRef ref2 = ces.createPositionRef(end, Bias.Forward);
    PositionBounds bounds = new PositionBounds(ref1, ref2);

    return new WhereUsedElement(bounds, html,
            fo, name, new OffsetRange(start, end), icon);
}
 
Example 14
Source File: WhereUsedElement.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public static WhereUsedElement create(FileObject fileObject, Entry entry, ElementKind kind, boolean related) {
    Icon icon = UiUtils.getElementIcon(kind, Collections.<Modifier>emptyList());
    String name = entry.getName();
    OffsetRange range = entry.getDocumentRange();

    int start = range.getStart();
    int end = range.getEnd();
    
    int sta = start;
    int en = start; // ! Same line as start
    String content = null;
    
    BaseDocument bdoc = GsfUtilities.getDocument(fileObject, true);
    try {
        bdoc.readLock();

        // I should be able to just call tree.getInfo().getText() to get cached
        // copy - but since I'm playing fast and loose with compilationinfos
        // for for example find subclasses (using a singly dummy FileInfo) I need
        // to read it here instead
        content = bdoc.getText(0, bdoc.getLength());
        sta = Utilities.getRowFirstNonWhite(bdoc, start);

        if (sta == -1) {
            sta = Utilities.getRowStart(bdoc, start);
        }

        en = Utilities.getRowLastNonWhite(bdoc, start);

        if (en == -1) {
            en = Utilities.getRowEnd(bdoc, start);
        } else {
            // Last nonwhite - left side of the last char, not inclusive
            en++;
        }

        // Sometimes the node we get from the AST is for the whole block
        // (e.g. such as the whole class), not the argument node. This happens
        // for example in Find Subclasses out of the index. In this case
        if (end > en) {
            end = start + name.length();

            if (end > bdoc.getLength()) {
                end = bdoc.getLength();
            }
        }
    } catch (Exception ex) {
        Exceptions.printStackTrace(ex);
    } finally {
        bdoc.readUnlock();
    }

    StringBuilder sb = new StringBuilder();
    if (end < sta) {
        // XXX Shouldn't happen, but I still have AST offset errors
        sta = end;
    }
    if (start < sta) {
        // XXX Shouldn't happen, but I still have AST offset errors
        start = sta;
    }
    if (en < end) {
        // XXX Shouldn't happen, but I still have AST offset errors
        en = end;
    }
    sb.append(encodeCharRefs(content.subSequence(sta, start).toString()));
    sb.append("<b>"); // NOI18N
    sb.append(content.subSequence(start, end));
    sb.append("</b>"); // NOI18N
    sb.append(encodeCharRefs(content.subSequence(end, en).toString()));
    if(!related) {
        sb.append(NbBundle.getMessage(WhereUsedElement.class, "MSG_Unrelated_Where_Used_Occurance")); //NOI18N
    }


    CloneableEditorSupport ces = GsfUtilities.findCloneableEditorSupport(fileObject);
    PositionRef ref1 = ces.createPositionRef(start, Bias.Forward);
    PositionRef ref2 = ces.createPositionRef(end, Bias.Forward);
    PositionBounds bounds = new PositionBounds(ref1, ref2);

    return new WhereUsedElement(bounds, sb.toString().trim(), 
            fileObject, name, new OffsetRange(start, end), icon);
}
 
Example 15
Source File: WhereUsedElement.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public static WhereUsedElement create(FileObject fileObject, String name, OffsetRange range, ElementKind kind) {
    Icon icon = UiUtils.getElementIcon(kind, Collections.<Modifier>emptyList());

    int start = range.getStart();
    int end = range.getEnd();

    int sta = start;
    int en = start; // ! Same line as start
    String content = null;

    BaseDocument bdoc = GsfUtilities.getDocument(fileObject, true);
    StringBuilder displayText = new StringBuilder();
    try {
        bdoc.readLock();

        // I should be able to just call tree.getInfo().getText() to get cached
        // copy - but since I'm playing fast and loose with compilationinfos
        // for for example find subclasses (using a singly dummy FileInfo) I need
        // to read it here instead
        content = bdoc.getText(0, bdoc.getLength());
        sta = Utilities.getRowFirstNonWhite(bdoc, start);

        if (sta == -1) {
            sta = Utilities.getRowStart(bdoc, start);
        }

        en = Utilities.getRowLastNonWhite(bdoc, start);

        if (en == -1) {
            en = Utilities.getRowEnd(bdoc, start);
        } else {
            // Last nonwhite - left side of the last char, not inclusive
            en++;
        }

        // Sometimes the node we get from the AST is for the whole block
        // (e.g. such as the whole class), not the argument node. This happens
        // for example in Find Subclasses out of the index. In this case
        if (end > en) {
            end = start + name.length();

            if (end > bdoc.getLength()) {
                end = bdoc.getLength();
            }
        }
    } catch (Exception ex) {
        content = "n/a"; //NOI18N
        Exceptions.printStackTrace(ex);
    } finally {
        bdoc.readUnlock();
    }

    if (end < sta) {
        // XXX Shouldn't happen, but I still have AST offset errors
        sta = end;
    }
    if (start < sta) {
        // XXX Shouldn't happen, but I still have AST offset errors
        start = sta;
    }
    if (en < end) {
        // XXX Shouldn't happen, but I still have AST offset errors
        en = end;
    }
    
    displayText.append(encodeCharRefs(content.subSequence(sta, start).toString()));
    displayText.append("<b>"); // NOI18N
    displayText.append(content.subSequence(start, end));
    displayText.append("</b>"); // NOI18N
    displayText.append(encodeCharRefs(content.subSequence(end, en).toString()));
  
    CloneableEditorSupport ces = GsfUtilities.findCloneableEditorSupport(fileObject);
    PositionRef ref1 = ces.createPositionRef(start, Bias.Forward);
    PositionRef ref2 = ces.createPositionRef(end, Bias.Forward);
    PositionBounds bounds = new PositionBounds(ref1, ref2);

    return new WhereUsedElement(bounds, displayText.toString().trim(),
            fileObject, name, new OffsetRange(start, end), icon);
}