Search in sources :

Example 51 with IStorage

use of org.eclipse.core.resources.IStorage in project linuxtools by eclipse.

the class PrepareChangeLogAction method getChangedLines.

private void getChangedLines(Subscriber s, PatchFile p, IProgressMonitor monitor) {
    try {
        // For an outgoing changed resource, find out which lines
        // differ from the local file and its previous local version
        // (i.e. we don't want to force a diff with the repository).
        IDiff d = s.getDiff(p.getResource());
        if (d instanceof IThreeWayDiff && ((IThreeWayDiff) d).getDirection() == IThreeWayDiff.OUTGOING) {
            IThreeWayDiff diff = (IThreeWayDiff) d;
            monitor.beginTask(null, 100);
            IResourceDiff localDiff = (IResourceDiff) diff.getLocalChange();
            IResource resource = localDiff.getResource();
            if (resource instanceof IFile) {
                IFile file = (IFile) resource;
                // $NON-NLS-1$
                monitor.subTask(Messages.getString("ChangeLog.MergingDiffs"));
                String osEncoding = file.getCharset();
                IFileRevision ancestorState = localDiff.getBeforeState();
                IStorage ancestorStorage;
                if (ancestorState != null) {
                    ancestorStorage = ancestorState.getStorage(monitor);
                    p.setStorage(ancestorStorage);
                } else {
                    return;
                }
                try {
                    // We compare using a standard differencer to get ranges
                    // of changes.  We modify them to be document-based (i.e.
                    // first line is line 1) and store them for later parsing.
                    LineComparator left = new LineComparator(ancestorStorage.getContents(), osEncoding);
                    LineComparator right = new LineComparator(file.getContents(), osEncoding);
                    for (RangeDifference tmp : RangeDifferencer.findDifferences(left, right)) {
                        if (tmp.kind() == RangeDifference.CHANGE) {
                            // Right side of diff are all changes found in local file.
                            int rightLength = tmp.rightLength() > 0 ? tmp.rightLength() : tmp.rightLength() + 1;
                            // We also want to store left side of the diff which are changes to the ancestor as it may contain
                            // functions/methods that have been removed.
                            int leftLength = tmp.leftLength() > 0 ? tmp.leftLength() : tmp.leftLength() + 1;
                            // Only store left side changes if the storage exists and we add one to the start line number
                            if (p.getStorage() != null)
                                p.addLineRange(tmp.leftStart(), tmp.leftStart() + leftLength, false);
                            p.addLineRange(tmp.rightStart(), tmp.rightStart() + rightLength, true);
                        }
                    }
                } catch (UnsupportedEncodingException e) {
                // do nothing for now
                }
            }
            monitor.done();
        }
    } catch (CoreException e) {
    // Do nothing if error occurs
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IFileRevision(org.eclipse.team.core.history.IFileRevision) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IDiff(org.eclipse.team.core.diff.IDiff) IResourceDiff(org.eclipse.team.core.mapping.IResourceDiff) IStorage(org.eclipse.core.resources.IStorage) RangeDifference(org.eclipse.compare.rangedifferencer.RangeDifference) LineComparator(org.eclipse.linuxtools.internal.changelog.core.LineComparator) CoreException(org.eclipse.core.runtime.CoreException) IThreeWayDiff(org.eclipse.team.core.diff.IThreeWayDiff) IResource(org.eclipse.core.resources.IResource)

Example 52 with IStorage

use of org.eclipse.core.resources.IStorage in project linuxtools by eclipse.

the class PrepareCommitHandler method loadClipboard.

private void loadClipboard(IProgressMonitor monitor) {
    IEditorPart currentEditor;
    try {
        currentEditor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
    } catch (Exception e) {
        // no editor is active now so do nothing
        return;
    }
    if (currentEditor == null) {
        return;
    }
    IFile changelog = getChangelogFile(getDocumentLocation(currentEditor, false));
    if (changelog == null) {
        return;
    }
    String diffResult = "";
    IProject project = null;
    IResource[] resources = new IResource[] { changelog };
    project = changelog.getProject();
    RepositoryProvider r = RepositoryProvider.getProvider(project);
    if (r == null) {
        // There is no repository provider for this project, i.e
        return;
    // it's not shared.
    }
    SyncInfoSet set = new SyncInfoSet();
    Subscriber s = r.getSubscriber();
    try {
        s.refresh(resources, IResource.DEPTH_ZERO, monitor);
    } catch (TeamException e1) {
    // Ignore, continue anyways
    }
    s.collectOutOfSync(resources, IResource.DEPTH_ZERO, set, monitor);
    SyncInfo[] infos = set.getSyncInfos();
    if (infos.length == 1) {
        int kind = SyncInfo.getChange(infos[0].getKind());
        if (kind == SyncInfo.CHANGE) {
            try {
                IDiff d = s.getDiff(infos[0].getLocal());
                if (d instanceof IThreeWayDiff && ((IThreeWayDiff) d).getDirection() == IThreeWayDiff.OUTGOING) {
                    IThreeWayDiff diff = (IThreeWayDiff) d;
                    monitor.beginTask(null, 100);
                    IResourceDiff localDiff = (IResourceDiff) diff.getLocalChange();
                    IFile file = (IFile) localDiff.getResource();
                    monitor.subTask(Messages.getString(// $NON-NLS-1$
                    "ChangeLog.MergingDiffs"));
                    String osEncoding = file.getCharset();
                    IFileRevision ancestorState = localDiff.getBeforeState();
                    IStorage ancestorStorage;
                    if (ancestorState != null)
                        ancestorStorage = ancestorState.getStorage(monitor);
                    else {
                        ancestorStorage = null;
                        return;
                    }
                    try {
                        LineComparator left = new LineComparator(ancestorStorage.getContents(), osEncoding);
                        LineComparator right = new LineComparator(file.getContents(), osEncoding);
                        for (RangeDifference tmp : RangeDifferencer.findDifferences(left, right)) {
                            if (tmp.kind() == RangeDifference.CHANGE) {
                                LineNumberReader l = new LineNumberReader(new InputStreamReader(file.getContents()));
                                int rightLength = tmp.rightLength() > 0 ? tmp.rightLength() : tmp.rightLength() + 1;
                                String line0 = null;
                                String preDiffResult = "";
                                for (int i = 0; i < tmp.rightStart(); ++i) {
                                    // usage if needed.
                                    try {
                                        String line = l.readLine();
                                        if (line0 == null)
                                            line0 = line;
                                        preDiffResult += line + "\n";
                                    } catch (IOException e) {
                                        break;
                                    }
                                }
                                for (int i = 0; i < rightLength; ++i) {
                                    try {
                                        String line = l.readLine();
                                        // used as a commit comment.
                                        if (i == rightLength - tmp.rightStart()) {
                                            if (tmp.rightStart() != 0 && line.equals(line0)) {
                                                diffResult = preDiffResult += diffResult;
                                                // stop
                                                i = rightLength;
                                            // loop
                                            } else
                                                diffResult += line + "\n";
                                        } else
                                            // $NON-NLS-1$
                                            diffResult += line + "\n";
                                    } catch (IOException e) {
                                    // do nothing
                                    }
                                }
                            }
                        }
                    } catch (UnsupportedEncodingException e) {
                    // do nothing for now
                    }
                    monitor.done();
                }
            } catch (CoreException e) {
            // do nothing
            }
        }
    }
    if (!diffResult.equals(""))
        populateClipboardBuffer(diffResult);
}
Also used : IFile(org.eclipse.core.resources.IFile) IDiff(org.eclipse.team.core.diff.IDiff) LineNumberReader(java.io.LineNumberReader) TeamException(org.eclipse.team.core.TeamException) RangeDifference(org.eclipse.compare.rangedifferencer.RangeDifference) LineComparator(org.eclipse.linuxtools.internal.changelog.core.LineComparator) Subscriber(org.eclipse.team.core.subscribers.Subscriber) SyncInfo(org.eclipse.team.core.synchronize.SyncInfo) IThreeWayDiff(org.eclipse.team.core.diff.IThreeWayDiff) SyncInfoSet(org.eclipse.team.core.synchronize.SyncInfoSet) InputStreamReader(java.io.InputStreamReader) IFileRevision(org.eclipse.team.core.history.IFileRevision) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IEditorPart(org.eclipse.ui.IEditorPart) IResourceDiff(org.eclipse.team.core.mapping.IResourceDiff) IOException(java.io.IOException) IStorage(org.eclipse.core.resources.IStorage) CoreException(org.eclipse.core.runtime.CoreException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) TeamException(org.eclipse.team.core.TeamException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IProject(org.eclipse.core.resources.IProject) CoreException(org.eclipse.core.runtime.CoreException) RepositoryProvider(org.eclipse.team.core.RepositoryProvider) IResource(org.eclipse.core.resources.IResource)

Example 53 with IStorage

use of org.eclipse.core.resources.IStorage 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 54 with IStorage

use of org.eclipse.core.resources.IStorage 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)

Example 55 with IStorage

use of org.eclipse.core.resources.IStorage in project yamcs-studio by yamcs.

the class ImageIconDecorator method decorateImage.

@Override
public Image decorateImage(Image image, Object element) {
    if (!(element instanceof IStorage))
        return null;
    final IStorage stor = (IStorage) element;
    final IPath path = stor.getFullPath();
    try {
        ImageData data = null;
        final String ext = path.getFileExtension();
        if (GIF_EXT.equalsIgnoreCase(ext) || PNG_EXT.equalsIgnoreCase(ext) || ICO_EXT.equalsIgnoreCase(ext)) {
            ImageData tmpData = new ImageData(stor.getContents());
            data = tmpData.scaledTo(MAX_ICON_WIDTH, MAX_ICON_HEIGHT);
        } else if (SVG_EXT.equalsIgnoreCase(ext)) {
            data = SVGUtils.loadSVG(path, stor.getContents(), MAX_ICON_WIDTH, MAX_ICON_HEIGHT);
        }
        if (data != null && data.width <= MAX_ICON_WIDTH && data.height <= MAX_ICON_HEIGHT) {
            Image img = new Image(Display.getCurrent(), data);
            createdImages.add(img);
            return img;
        }
    } catch (Exception e) {
        Logger.getLogger(getClass().getName()).log(Level.WARNING, "Cannot create icon for " + path, e);
    }
    return null;
}
Also used : IPath(org.eclipse.core.runtime.IPath) ImageData(org.eclipse.swt.graphics.ImageData) IStorage(org.eclipse.core.resources.IStorage) Image(org.eclipse.swt.graphics.Image)

Aggregations

IStorage (org.eclipse.core.resources.IStorage)96 URI (org.eclipse.emf.common.util.URI)32 IFile (org.eclipse.core.resources.IFile)31 IProject (org.eclipse.core.resources.IProject)27 CoreException (org.eclipse.core.runtime.CoreException)25 IResource (org.eclipse.core.resources.IResource)20 Pair (org.eclipse.xtext.util.Pair)16 Test (org.junit.Test)15 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)13 IStorageEditorInput (org.eclipse.ui.IStorageEditorInput)12 IPath (org.eclipse.core.runtime.IPath)11 IJavaProject (org.eclipse.jdt.core.IJavaProject)10 IOException (java.io.IOException)7 WrappedException (org.eclipse.emf.common.util.WrappedException)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputStream (java.io.InputStream)6 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)6 GitSynchronizeData (org.eclipse.egit.core.synchronize.dto.GitSynchronizeData)6 GitSynchronizeDataSet (org.eclipse.egit.core.synchronize.dto.GitSynchronizeDataSet)6 Storage2UriMapperJavaImpl (org.eclipse.xtext.ui.resource.Storage2UriMapperJavaImpl)6