Example usage for org.eclipse.jdt.core IBuffer getContents

List of usage examples for org.eclipse.jdt.core IBuffer getContents

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IBuffer getContents.

Prototype

public String getContents();

Source Link

Document

Returns the contents of this buffer as a String.

Usage

From source file:com.codenvy.ide.ext.java.server.internal.core.ClassFile.java

License:Open Source License

/**
 * @see org.eclipse.jdt.core.ISourceReference
 *//*  www .  j a v a  2s  . c om*/
public String getSource() throws JavaModelException {
    IBuffer buffer = getBuffer();
    if (buffer == null) {
        return null;
    }
    return buffer.getContents();
}

From source file:com.codenvy.ide.ext.java.server.internal.core.ClassFile.java

License:Open Source License

/**
 * @see org.eclipse.jdt.core.ISourceReference
 *//*from   www  . j  a v a  2 s .  co m*/
public ISourceRange getSourceRange() throws JavaModelException {
    IBuffer buffer = getBuffer();
    if (buffer != null) {
        String contents = buffer.getContents();
        if (contents == null)
            return null;
        return new SourceRange(0, contents.length());
    } else {
        return null;
    }
}

From source file:com.codenvy.ide.ext.java.server.internal.core.CompilationUnit.java

License:Open Source License

/**
 * @see org.eclipse.jdt.core.ISourceReference#getSource()
 *//*  w ww.j av a 2  s. co  m*/
public String getSource() throws JavaModelException {
    IBuffer buffer = getBuffer();
    if (buffer == null)
        return ""; //$NON-NLS-1$
    return buffer.getContents();
}

From source file:com.microsoft.javapkgsrv.JavaParser.java

License:MIT License

public List<FindDefinitionResponse.JavaElement> ProcessFindDefinintionRequest(String fileParseContents,
        String typeRootId, int cursorPosition) throws Exception {
    if (ActiveTypeRoots.containsKey(typeRootId)) {
        ITypeRoot cu = ActiveTypeRoots.get(typeRootId);
        cu.getBuffer().setContents(fileParseContents.toCharArray());
        IJavaElement[] elements = cu.codeSelect(cursorPosition, 0);

        List<FindDefinitionResponse.JavaElement> ret = new ArrayList<FindDefinitionResponse.JavaElement>();
        for (IJavaElement element : elements) {
            String definition = element.toString();
            String path = element.getResource() != null ? element.getResource().getLocation().toOSString()
                    : element.getPath().toOSString();
            //String path = element.getPath().makeAbsolute().toOSString(); // element.getPath().toString();

            boolean isAvailable = false;
            int posStart = -1;
            int posLength = 0;
            String contents = null;
            String classFileName = null;
            IClassFile classFileObj = null;

            ISourceReference srcRef = (ISourceReference) element;
            if (srcRef != null) {
                ISourceRange range = srcRef.getSourceRange();
                if (SourceRange.isAvailable(range)) {
                    isAvailable = true;// www .  j a va2 s  .c  o m
                    posStart = range.getOffset();
                    posLength = range.getLength();

                    //if (path.endsWith(".jar"))
                    //{
                    IOpenable op = element.getOpenable();
                    if (op != null && op instanceof IClassFile) {
                        IBuffer buff = op.getBuffer();
                        classFileObj = (IClassFile) op;
                        classFileName = classFileObj.getElementName();
                        contents = buff.getContents();
                    }
                    //}
                }
            }

            FindDefinitionResponse.JavaElement.Builder retItem = FindDefinitionResponse.JavaElement.newBuilder()
                    .setDefinition(definition).setFilePath(path).setHasSource(isAvailable)
                    .setPositionStart(posStart).setPositionLength(posLength);

            if (contents != null) {
                //int hashCode = classFileObj.hashCode();
                String handle = classFileObj.getHandleIdentifier();
                ActiveTypeRoots.put(handle, classFileObj);

                retItem.setFileName(classFileName);
                retItem.setTypeRootIdentifier(TypeRootIdentifier.newBuilder().setHandle(handle).build());
            }
            System.out.println(retItem.toString());
            if (contents != null) {
                retItem.setFileContents(contents);
            }
            ret.add(retItem.build());
        }
        return ret;
    }
    return null;
}

From source file:edu.buffalo.cse.green.util.IModifiableBuffer.java

License:Open Source License

public IModifiableBuffer(IBuffer buffer) {
    setTextStore(new BufferModifierStore(buffer));
    ILineTracker lineTracker = new DefaultLineTracker();
    lineTracker.set(buffer.getContents());
    setLineTracker(lineTracker);/* w w w  .j  ava2  s.  co m*/
    completeInitialization();
}

From source file:org.codehaus.jdt.groovy.model.GroovyClassFileWorkingCopy.java

License:Open Source License

/**
 * @see Openable#openBuffer(IProgressMonitor, Object)
 *///from w w  w  .ja  v  a 2  s . c  o  m
protected IBuffer openBuffer(IProgressMonitor pm, Object info) throws JavaModelException {

    // create buffer
    IBuffer buffer = this.owner.createBuffer(this);
    if (buffer == null)
        return null;

    // set the buffer source
    if (buffer.getCharacters() == null) {
        IBuffer classFileBuffer = this.classFile.getBuffer();
        if (classFileBuffer != null) {
            buffer.setContents(classFileBuffer.getCharacters());
        } else {
            // Disassemble
            IClassFileReader reader = ToolFactory.createDefaultClassFileReader(this.classFile,
                    IClassFileReader.ALL);
            Disassembler disassembler = new Disassembler();
            String contents = disassembler.disassemble(reader, Util.getLineSeparator("", getJavaProject()), //$NON-NLS-1$
                    ClassFileBytesDisassembler.WORKING_COPY);
            buffer.setContents(contents);
        }
    }

    // add buffer to buffer cache
    BufferManager bufManager = getBufferManager();

    // GROOVY Change access to private member
    // old
    // bufManager.addBuffer(buffer);
    // new
    if (buffer.getContents() != null) {
        ReflectionUtils.executePrivateMethod(BufferManager.class, "addBuffer", new Class<?>[] { IBuffer.class }, //$NON-NLS-1$
                bufManager, new Object[] { buffer });
    }
    // GROOVY End

    // listen to buffer changes
    buffer.addBufferChangedListener(this);

    return buffer;
}

From source file:org.eclim.plugin.jdt.util.JavaUtils.java

License:Open Source License

/**
 * Format a region in the supplied source file.
 *
 * @param src The ICompilationUnit./*from  w w  w.j a  va 2 s  . c om*/
 * @param kind The kind of code snippet to format.
 * @param offset The starting offset of the region to format.
 * @param length The length of the region to format.
 */
public static void format(ICompilationUnit src, int kind, int offset, int length) throws Exception {
    IBuffer buffer = src.getBuffer();
    String contents = buffer.getContents();
    String delimiter = StubUtility.getLineDelimiterUsed(src);
    DefaultCodeFormatter formatter = new DefaultCodeFormatter(src.getJavaProject().getOptions(true));

    // when the eclipse indent settings differ from vim (tabs vs spaces) then
    // the inserted method's indent may be a bit off. this is a workaround to
    // force reformatting of the code from the start of the line to the start of
    // the next set of code following the new method. Doesn't quite fix indent
    // formatting of methods in nested classes.
    while (offset > 0 && !IndentManipulation.isLineDelimiterChar(buffer.getChar(offset - 1))) {
        offset--;
        length++;
    }
    while ((offset + length) < contents.length()
            && IndentManipulation.isLineDelimiterChar(buffer.getChar(offset + length))) {
        length++;
    }

    TextEdit edits = formatter.format(kind, contents, offset, length, 0, delimiter);
    if (edits != null) {
        int oldLength = contents.length();
        Document document = new Document(contents);
        edits.apply(document);

        String formatted = document.get();

        // jdt formatter can introduce trailing whitespace (javadoc comments), so
        // we'll remove all trailing whitespace from the formatted section (unless
        // the user has configured eclim not to do so).
        length += formatted.length() - oldLength;
        if (offset < (offset + length)) {
            String stripTrailingWhitespace = Preferences.getInstance().getValue(
                    src.getJavaProject().getProject(), "org.eclim.java.format.strip_trialing_whitespace");
            if ("true".equals(stripTrailingWhitespace)) {
                String pre = formatted.substring(0, offset);
                StringBuffer section = new StringBuffer(formatted.substring(offset, offset + length));
                StringBuffer post = new StringBuffer(formatted.substring(offset + length));
                // account for section not ending at a line delimiter
                while (!section.toString().endsWith(delimiter) && post.length() > 0) {
                    section.append(post.charAt(0));
                    post.deleteCharAt(0);
                }

                Matcher matcher = TRAILING_WHITESPACE.matcher(section);
                String stripped = matcher.replaceAll(StringUtils.EMPTY);

                src.getBuffer().setContents(pre + stripped + post);
            } else {
                src.getBuffer().setContents(formatted);
            }
        } else {
            src.getBuffer().setContents(formatted);
        }

        if (src.isWorkingCopy()) {
            src.commitWorkingCopy(true, null);
        }
        src.save(null, false);
    }
}

From source file:org.eclipse.ajdt.core.javaelements.AJCodeElement.java

License:Open Source License

public void initializeLocations() {
    // try the easy way:
    IProgramElement ipe = AJProjectModelFactory.getInstance().getModelForJavaElement(this)
            .javaElementToProgramElement(this);
    ISourceLocation sloc = ipe.getSourceLocation();
    if (sloc != null) {
        startLine = sloc.getLine();//from w  w w. j  av a  2s.c  om

        nameStart = sloc.getOffset();
        if (sloc instanceof EclipseSourceLocation) {
            EclipseSourceLocation esloc = (EclipseSourceLocation) sloc;
            nameEnd = esloc.getEndPos();
        }
    }

    // sometimes the start and end values are not set...so do it the hard way
    // so calculate it from the line
    if (nameStart <= 0 || nameEnd <= 0) {
        try {
            IOpenable openable = this.parent.getOpenableParent();
            IBuffer buffer;
            if (openable instanceof AJCompilationUnit) {
                AJCompilationUnit ajCompUnit = (AJCompilationUnit) openable;
                ajCompUnit.requestOriginalContentMode();
                buffer = openable.getBuffer();
                ajCompUnit.discardOriginalContentMode();
            } else {
                buffer = openable.getBuffer();
            }
            String source = buffer.getContents();

            int lines = 0;
            for (int i = 0; i < source.length(); i++) {
                if (source.charAt(i) == '\n') {
                    lines++;
                    if (lines == startLine - 1) {
                        // starting remove white space
                        i++;
                        while (i < source.length()
                                && (Character.isWhitespace(source.charAt(i)) && source.charAt(i) != '\n')) {
                            i++;
                        }
                        nameStart = i;
                        break;
                    }
                }
            }

            for (int i = nameStart + 1; i < source.length(); i++) {
                if (source.charAt(i) == '\n' || source.charAt(i) == ';') {
                    nameEnd = i - 1;
                    break;
                }
            }

            nameStart = Math.min(nameStart, nameEnd);
        } catch (JavaModelException e) {
        }
    }
}

From source file:org.eclipse.che.jdt.internal.core.Openable.java

License:Open Source License

public String findRecommendedLineSeparator() throws JavaModelException {
    IBuffer buffer = getBuffer();
    String source = buffer == null ? null : buffer.getContents();
    return Util.getLineSeparator(source, getJavaProject());
}

From source file:org.eclipse.wb.internal.rcp.wizards.jface.wizard.WizardWizardPage.java

License:Open Source License

private void addPageInvocations(IType newType, ImportsManager imports) throws JavaModelException {
    IBuffer buffer = newType.getCompilationUnit().getBuffer();
    // prepare addPage() invocations
    String addPagesSource = "";
    for (IType page : getSelectedPages()) {
        String simpleName = imports.addImport(page.getFullyQualifiedName());
        addPagesSource += "addPage(new " + simpleName + "());";
    }//from  w ww  .ja  v a  2s  . c o  m
    // prepare position for addPage() invocations
    int pagesOffset;
    {
        IMethod pagesMethod = newType.getMethod("addPages", new String[0]);
        pagesOffset = pagesMethod.getSourceRange().getOffset();
        pagesOffset = buffer.getContents().indexOf('{', pagesOffset) + 1;
    }
    // insert addPage() invocations, don't care about formatting
    buffer.replace(pagesOffset, 0, addPagesSource);
}