Search in sources :

Example 1 with IStorageEditorInput

use of org.eclipse.ui.IStorageEditorInput in project eclipse.platform.text by eclipse.

the class LastSaveReferenceProvider method readDocument.

/**
 * Reads in the saved document into <code>fReference</code>.
 *
 * @param monitor a progress monitor, or <code>null</code>
 * @param force <code>true</code> if the reference document should also
 *        be read if the current document is <code>null</code>,<code>false</code>
 *        if it should only be updated if it already existed.
 */
private void readDocument(IProgressMonitor monitor, boolean force) {
    // protect against concurrent disposal
    IDocumentProvider prov = fDocumentProvider;
    IEditorInput inp = fEditorInput;
    IDocument doc = fReference;
    ITextEditor editor = fEditor;
    if (prov instanceof IStorageDocumentProvider && inp instanceof IStorageEditorInput) {
        IStorageEditorInput input = (IStorageEditorInput) inp;
        IStorageDocumentProvider provider = (IStorageDocumentProvider) prov;
        if (doc == null)
            if (force || fDocumentRead)
                doc = new Document();
            else
                return;
        IJobManager jobMgr = Job.getJobManager();
        try {
            IStorage storage = input.getStorage();
            // check for null for backward compatibility (we used to check before...)
            if (storage == null)
                return;
            fProgressMonitor = monitor;
            ISchedulingRule rule = getSchedulingRule(storage);
            // delay for any other job requiring the lock on file
            try {
                lockDocument(monitor, jobMgr, rule);
                String encoding;
                if (storage instanceof IEncodedStorage)
                    encoding = ((IEncodedStorage) storage).getCharset();
                else
                    encoding = null;
                boolean skipUTF8BOM = isUTF8BOM(encoding, storage);
                setDocumentContent(doc, storage, encoding, monitor, skipUTF8BOM);
            } finally {
                unlockDocument(jobMgr, rule);
                fProgressMonitor = null;
            }
        } catch (CoreException e) {
            return;
        }
        if (monitor != null && monitor.isCanceled())
            return;
        // update state
        synchronized (fLock) {
            if (fDocumentProvider == provider && fEditorInput == input) {
                // only update state if our provider / input pair has not
                // been updated in between (dispose or setActiveEditor)
                fReference = doc;
                fDocumentRead = true;
                addElementStateListener(editor, prov);
            }
        }
    }
}
Also used : IStorageDocumentProvider(org.eclipse.ui.editors.text.IStorageDocumentProvider) IStorageEditorInput(org.eclipse.ui.IStorageEditorInput) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IEncodedStorage(org.eclipse.core.resources.IEncodedStorage) IJobManager(org.eclipse.core.runtime.jobs.IJobManager) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IStorage(org.eclipse.core.resources.IStorage) ISchedulingRule(org.eclipse.core.runtime.jobs.ISchedulingRule) IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) CoreException(org.eclipse.core.runtime.CoreException) IEditorInput(org.eclipse.ui.IEditorInput) IDocument(org.eclipse.jface.text.IDocument)

Example 2 with IStorageEditorInput

use of org.eclipse.ui.IStorageEditorInput in project eclipse.platform.text by eclipse.

the class StorageDocumentProvider method createElementInfo.

@Override
protected ElementInfo createElementInfo(Object element) throws CoreException {
    if (element instanceof IStorageEditorInput) {
        IDocument document = null;
        IStatus status = null;
        try {
            document = createDocument(element);
        } catch (CoreException x) {
            status = x.getStatus();
            document = createEmptyDocument();
        }
        ElementInfo info = new StorageInfo(document, createAnnotationModel(element));
        info.fStatus = status;
        ((StorageInfo) info).fEncoding = getPersistedEncoding(element);
        return info;
    }
    return super.createElementInfo(element);
}
Also used : IStorageEditorInput(org.eclipse.ui.IStorageEditorInput) IStatus(org.eclipse.core.runtime.IStatus) CoreException(org.eclipse.core.runtime.CoreException) IDocument(org.eclipse.jface.text.IDocument)

Example 3 with IStorageEditorInput

use of org.eclipse.ui.IStorageEditorInput in project eclipse.platform.text by eclipse.

the class StorageDocumentProvider method getContentType.

@Override
public IContentType getContentType(Object element) throws CoreException {
    if (element instanceof IStorageEditorInput) {
        IStorage storage = ((IStorageEditorInput) element).getStorage();
        try {
            IContentDescription desc;
            IDocument document = getDocument(element);
            if (document != null) {
                try (Reader reader = new DocumentReader(document)) {
                    desc = Platform.getContentTypeManager().getDescriptionFor(reader, storage.getName(), NO_PROPERTIES);
                }
            } else {
                try (InputStream stream = storage.getContents()) {
                    desc = Platform.getContentTypeManager().getDescriptionFor(stream, storage.getName(), NO_PROPERTIES);
                }
            }
            if (desc != null && desc.getContentType() != null)
                return desc.getContentType();
        } catch (IOException x) {
            IPath path = storage.getFullPath();
            String name;
            if (path != null)
                name = path.toOSString();
            else
                name = storage.getName();
            String message;
            if (name != null)
                message = NLSUtility.format(TextEditorMessages.StorageDocumentProvider_getContentDescriptionFor, name);
            else
                message = TextEditorMessages.StorageDocumentProvider_getContentDescription;
            throw new CoreException(new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, IStatus.OK, message, x));
        }
    }
    return super.getContentType(element);
}
Also used : MultiStatus(org.eclipse.core.runtime.MultiStatus) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IResourceStatus(org.eclipse.core.resources.IResourceStatus) IStorageEditorInput(org.eclipse.ui.IStorageEditorInput) IPath(org.eclipse.core.runtime.IPath) CoreException(org.eclipse.core.runtime.CoreException) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) IStorage(org.eclipse.core.resources.IStorage) IContentDescription(org.eclipse.core.runtime.content.IContentDescription) IDocument(org.eclipse.jface.text.IDocument)

Example 4 with IStorageEditorInput

use of org.eclipse.ui.IStorageEditorInput in project linuxtools by eclipse.

the class CParser method parseCurrentFunction.

@Override
public String parseCurrentFunction(IEditorInput input, int offset) throws CoreException {
    String currentElementName;
    if (input instanceof IFileEditorInput) {
        // Get the working copy and connect to input.
        IWorkingCopyManager manager = CUIPlugin.getDefault().getWorkingCopyManager();
        manager.connect(input);
        // Retrieve the C/C++ Element in question.
        IWorkingCopy workingCopy = manager.getWorkingCopy(input);
        ICElement method = workingCopy.getElementAtOffset(offset);
        manager.disconnect(input);
        // no element selected
        if (method == null)
            return "";
        // Get the current element name, to test it.
        currentElementName = method.getElementName();
        // Element doesn't have a name. Can go no further.
        if (currentElementName == null) {
            // element doesn't have a name
            return "";
        }
        // Get the Element Type to test.
        int elementType = method.getElementType();
        switch(elementType) {
            case ICElement.C_FIELD:
            case ICElement.C_METHOD:
            case ICElement.C_FUNCTION:
                break;
            case ICElement.C_MODEL:
                return "";
            // So it's not a method, field, function, or model. Where are we?
            default:
                ICElement tmpMethodType;
                if (((tmpMethodType = method.getAncestor(ICElement.C_FUNCTION)) == null) && ((tmpMethodType = method.getAncestor(ICElement.C_METHOD)) == null) && ((tmpMethodType = method.getAncestor(ICElement.C_CLASS)) == null)) {
                    return "";
                } else {
                    // In a class, but not in a method. Return class name instead.
                    method = tmpMethodType;
                    currentElementName = method.getElementName();
                }
        }
        // Build all ancestor classes.
        // Append all ancestor class names to string
        ICElement tmpParent = method.getParent();
        while (tmpParent != null) {
            ICElement tmpParentClass = tmpParent.getAncestor(ICElement.C_CLASS);
            if (tmpParentClass != null) {
                String tmpParentClassName = tmpParentClass.getElementName();
                if (tmpParentClassName == null)
                    return currentElementName;
                currentElementName = tmpParentClassName + "." + currentElementName;
            } else
                return currentElementName;
            tmpParent = tmpParentClass.getParent();
        }
        return currentElementName;
    } else if (input instanceof IStorageEditorInput) {
        // Get the working copy and connect to input.
        // don't follow inclusions
        currentElementName = "";
        IStorageEditorInput sei = (IStorageEditorInput) input;
        // don't follow inclusions
        IncludeFileContentProvider contentProvider = IncludeFileContentProvider.getEmptyFilesProvider();
        // empty scanner info
        IScannerInfo scanInfo = new ScannerInfo();
        IStorage ancestorStorage = sei.getStorage();
        if (ancestorStorage == null)
            return "";
        InputStream stream = ancestorStorage.getContents();
        byte[] buffer = new byte[100];
        String data = "";
        int read = 0;
        try {
            do {
                read = stream.read(buffer);
                if (read > 0) {
                    String tmp = new String(buffer, 0, read);
                    data = data.concat(tmp);
                }
            } while (read == 100);
            stream.close();
        } catch (IOException e) {
        // do nothing
        }
        // $NON-NLS-1$
        FileContent content = FileContent.create("<text>", data.toCharArray());
        // determine the language
        ILanguage language = GPPLanguage.getDefault();
        try {
            IASTTranslationUnit ast;
            int options = 0;
            ast = language.getASTTranslationUnit(content, scanInfo, contentProvider, null, options, ParserUtil.getParserLogService());
            IASTNodeSelector n = ast.getNodeSelector(null);
            IASTNode node = n.findFirstContainedNode(offset, 100);
            while (node != null && !(node instanceof IASTTranslationUnit)) {
                if (node instanceof IASTFunctionDefinition) {
                    IASTFunctionDefinition fd = (IASTFunctionDefinition) node;
                    IASTFunctionDeclarator d = fd.getDeclarator();
                    currentElementName = new String(d.getName().getSimpleID());
                    break;
                }
                node = node.getParent();
            }
        } catch (CoreException exc) {
            currentElementName = "";
            CUIPlugin.log(exc);
        }
        return currentElementName;
    }
    return "";
}
Also used : IScannerInfo(org.eclipse.cdt.core.parser.IScannerInfo) ScannerInfo(org.eclipse.cdt.core.parser.ScannerInfo) IncludeFileContentProvider(org.eclipse.cdt.core.parser.IncludeFileContentProvider) IWorkingCopy(org.eclipse.cdt.core.model.IWorkingCopy) IStorageEditorInput(org.eclipse.ui.IStorageEditorInput) IASTFunctionDeclarator(org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator) InputStream(java.io.InputStream) IASTNode(org.eclipse.cdt.core.dom.ast.IASTNode) IOException(java.io.IOException) ICElement(org.eclipse.cdt.core.model.ICElement) IStorage(org.eclipse.core.resources.IStorage) IASTFunctionDefinition(org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition) FileContent(org.eclipse.cdt.core.parser.FileContent) ILanguage(org.eclipse.cdt.core.model.ILanguage) IASTNodeSelector(org.eclipse.cdt.core.dom.ast.IASTNodeSelector) CoreException(org.eclipse.core.runtime.CoreException) IFileEditorInput(org.eclipse.ui.IFileEditorInput) IASTTranslationUnit(org.eclipse.cdt.core.dom.ast.IASTTranslationUnit) IScannerInfo(org.eclipse.cdt.core.parser.IScannerInfo) IWorkingCopyManager(org.eclipse.cdt.ui.IWorkingCopyManager)

Example 5 with IStorageEditorInput

use of org.eclipse.ui.IStorageEditorInput in project linuxtools by eclipse.

the class CParserTest method canParseCurrentFunctionFromCStringInIStorageEditorInput.

/**
 * Given an IStorageEditorInput we should be able to retrieve the currently
 * active C function.
 *
 * @throws Exception
 */
@Test
public void canParseCurrentFunctionFromCStringInIStorageEditorInput() throws Exception {
    final String expectedFunctionName = "doSomething";
    final String cSourceCode = "static int " + expectedFunctionName + "(char *test)\n" + "{\n" + "int index = 0;\n" + "// " + OFFSET_MARKER + "\n" + "return 0;\n" + "}\n";
    // prepare IStorageEditorInput
    IStorage cStringStorage = new CStringStorage(cSourceCode);
    IStorageEditorInput cStringStorageEditorInput = new CStringStorageInput(cStringStorage);
    // Figure out the desired offset
    int selectOffset = cSourceCode.indexOf(OFFSET_MARKER);
    assertTrue(selectOffset >= 0);
    final String actualFunctionName = cParser.parseCurrentFunction(cStringStorageEditorInput, selectOffset);
    assertEquals(expectedFunctionName, actualFunctionName);
}
Also used : CStringStorage(org.eclipse.linuxtools.changelog.tests.fixtures.CStringStorage) IStorageEditorInput(org.eclipse.ui.IStorageEditorInput) CStringStorageInput(org.eclipse.linuxtools.changelog.tests.fixtures.CStringStorageInput) IStorage(org.eclipse.core.resources.IStorage) Test(org.junit.Test)

Aggregations

IStorageEditorInput (org.eclipse.ui.IStorageEditorInput)5 IStorage (org.eclipse.core.resources.IStorage)4 CoreException (org.eclipse.core.runtime.CoreException)4 IDocument (org.eclipse.jface.text.IDocument)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 IStatus (org.eclipse.core.runtime.IStatus)2 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 IASTFunctionDeclarator (org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator)1 IASTFunctionDefinition (org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition)1 IASTNode (org.eclipse.cdt.core.dom.ast.IASTNode)1 IASTNodeSelector (org.eclipse.cdt.core.dom.ast.IASTNodeSelector)1 IASTTranslationUnit (org.eclipse.cdt.core.dom.ast.IASTTranslationUnit)1 ICElement (org.eclipse.cdt.core.model.ICElement)1 ILanguage (org.eclipse.cdt.core.model.ILanguage)1 IWorkingCopy (org.eclipse.cdt.core.model.IWorkingCopy)1 FileContent (org.eclipse.cdt.core.parser.FileContent)1 IScannerInfo (org.eclipse.cdt.core.parser.IScannerInfo)1