From fb5c539efc8b0f3cb35cbc8c4414244be9dce84e Mon Sep 17 00:00:00 2001 From: Markus Schorn Date: Mon, 2 Feb 2009 16:27:44 +0000 Subject: [PATCH] Position trackers for non-local external files, bug 261912. --- .../cdt/core/IPositionTrackerManager.java | 29 +++++---- .../internal/core/PositionTrackerManager.java | 64 +++++++++++++++---- .../internal/ui/viewsupport/EditorOpener.java | 39 ++++++++++- 3 files changed, 106 insertions(+), 26 deletions(-) diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/IPositionTrackerManager.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/IPositionTrackerManager.java index e23f3b15af8..20c2f122a57 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/IPositionTrackerManager.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/core/IPositionTrackerManager.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2006, 2008 Wind River Systems, Inc. and others. + * Copyright (c) 2006, 2009 Wind River Systems, Inc. and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -8,9 +8,10 @@ * Contributors: * Markus Schorn - initial API and implementation *******************************************************************************/ - package org.eclipse.cdt.core; +import java.net.URI; + import org.eclipse.cdt.core.model.ITranslationUnit; import org.eclipse.core.resources.IFile; import org.eclipse.core.runtime.IPath; @@ -19,16 +20,10 @@ import org.eclipse.core.runtime.IPath; * An interface to manage the position tracking. It allows for mapping character * offsets from a file previously stored on disk to the offset in the current document * for the file. - * - *

This interface is not intended to be implemented by clients.

- * - *

- * EXPERIMENTAL. This interface has been added as - * part of a work in progress. There is no guarantee that this API will - * work or that it will remain the same. Please do not use this API without - * consulting with the CDT team. - *

* @since 4.0 + * + * @noextend This interface is not intended to be extended by clients. + * @noimplement This interface is not intended to be implemented by clients. */ public interface IPositionTrackerManager { /** @@ -61,4 +56,16 @@ public interface IPositionTrackerManager { * @return the requested position converter or null. */ public IPositionConverter findPositionConverter(IPath fullPathOrExternalLocation, long timestamp); + + /** + * Returns the position tracker suitable for mapping character offsets of the + * given external file/timestamp to the current version of it.

+ * The method cannot be used for resources that are part of the workspace. + * + * @param externalLocation an external location for which the position adapter is requested. + * @param timestamp identifies the version of the file stored on disk. + * @return the requested position converter or null. + * @since 5.1 + */ + public IPositionConverter findPositionConverter(URI externalLocation, long timestamp); } diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/PositionTrackerManager.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/PositionTrackerManager.java index 18bb0745ab2..9a44fe87a56 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/PositionTrackerManager.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/PositionTrackerManager.java @@ -10,6 +10,7 @@ *******************************************************************************/ package org.eclipse.cdt.internal.core; +import java.net.URI; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; @@ -25,6 +26,7 @@ import org.eclipse.core.filebuffers.IFileBuffer; import org.eclipse.core.filebuffers.IFileBufferListener; import org.eclipse.core.filebuffers.ITextFileBuffer; import org.eclipse.core.filebuffers.ITextFileBufferManager; +import org.eclipse.core.filesystem.URIUtil; import org.eclipse.core.resources.IFile; import org.eclipse.core.runtime.IPath; @@ -40,10 +42,16 @@ public class PositionTrackerManager implements IPositionTrackerManager, IFileBuf private int fMemoryCounter= 0; private int fInstalled= 0; - private HashMap fPositionTrackerMap; + /** + * as the key in the map we use: + * the full path for resources, + * the location as path for local non-workspace files, + * the location as URI for non-local non-workspace files. + */ + private HashMap fPositionTrackerMap; private PositionTrackerManager() { - fPositionTrackerMap= new HashMap(); + fPositionTrackerMap= new HashMap(); } public synchronized void install() { @@ -93,13 +101,13 @@ public class PositionTrackerManager implements IPositionTrackerManager, IFileBuf public void stateChanging(IFileBuffer buffer) {} private synchronized void createCheckpoint(ITextFileBuffer buffer) { - PositionTrackerChain chain= getChain(buffer); + final Object bufferKey= getKey(buffer); + PositionTrackerChain chain= fPositionTrackerMap.get(bufferKey); if (chain == null) { chain = new PositionTrackerChain(buffer.getModificationStamp()); - fPositionTrackerMap.put(buffer.getLocation(), chain); + fPositionTrackerMap.put(bufferKey, chain); fMemoryCounter+= PositionTrackerChain.MEMORY_SIZE + HASHMAP_ENTRY_SIZE; - } - else { + } else { chain.stopTracking(); fMemoryCounter+= chain.createCheckpoint(buffer.getModificationStamp()); } @@ -110,18 +118,27 @@ public class PositionTrackerManager implements IPositionTrackerManager, IFileBuf } } - private synchronized PositionTrackerChain getChain(ITextFileBuffer buffer) { - return fPositionTrackerMap.get(buffer.getLocation()); - } + private Object getKey(ITextFileBuffer buffer) { + Object key= buffer.getLocation(); + if (key == null) { + URI uri= buffer.getFileStore().toURI(); + key= URIUtil.toPath(uri); + if (key == null) { + key= uri; + } + } + return key; + } private synchronized void resetToLastCheckpoint(ITextFileBuffer buffer) { - PositionTrackerChain chain= getChain(buffer); + final Object bufferKey= getKey(buffer); + PositionTrackerChain chain= fPositionTrackerMap.get(bufferKey); if (chain != null) { chain.stopTracking(); chain.getActiveTracker().clear(); if (!chain.isModified()) { - fPositionTrackerMap.remove(buffer.getLocation()); + fPositionTrackerMap.remove(bufferKey); chain.dispose(); } } @@ -196,11 +213,34 @@ public class PositionTrackerManager implements IPositionTrackerManager, IFileBuf IFile file= (IFile) tu.getResource(); if (file != null) { return findPositionConverter(file, timestamp); - } + } IPath location= tu.getLocation(); if (location != null) { return findPositionConverter(location, timestamp); } + + URI locationURI = tu.getLocationURI(); + if (locationURI != null) { + return findPositionConverter(locationURI, timestamp); + } + return null; } + + /** + * {@inheritDoc} + */ + public synchronized IPositionConverter findPositionConverter(URI locationURI, long timestamp) { + PositionTrackerChain chain= fPositionTrackerMap.get(locationURI); + if (chain == null) { + IPath path= URIUtil.toPath(locationURI); + if (path != null) { + chain= fPositionTrackerMap.get(path); + } + } + if (chain != null) { + return chain.findTrackerAt(timestamp); + } + return null; + } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/viewsupport/EditorOpener.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/viewsupport/EditorOpener.java index bbd1ce17ea0..8e45922946a 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/viewsupport/EditorOpener.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/viewsupport/EditorOpener.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2006, 2007 Wind River Systems, Inc. and others. + * Copyright (c) 2006, 2009 Wind River Systems, Inc. and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -9,11 +9,14 @@ * Markus Schorn - initial API and implementation * Ed Swartz (Nokia) *******************************************************************************/ - package org.eclipse.cdt.internal.ui.viewsupport; +import java.net.URI; + +import org.eclipse.core.filesystem.EFS; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.jface.text.IRegion; import org.eclipse.jface.text.Region; @@ -38,7 +41,6 @@ import org.eclipse.cdt.internal.ui.util.EditorUtility; /** * An utility to open editors for references or elements. - * @author markus.schorn@windriver.com */ public class EditorOpener { @@ -70,6 +72,17 @@ public class EditorOpener { te.selectAndReveal(region.getOffset(), region.getLength()); } } + + private static void selectRegion(URI locationURI, IRegion region, long timestamp, IEditorPart editor) { + if (editor instanceof ITextEditor) { + ITextEditor te= (ITextEditor) editor; + IPositionConverter pc= CCorePlugin.getPositionTrackerManager().findPositionConverter(locationURI, timestamp); + if (pc != null) { + region= pc.historicToActual(region); + } + te.selectAndReveal(region.getOffset(), region.getLength()); + } + } /** * Opens the editor for an external location, selecting the given region. @@ -86,6 +99,26 @@ public class EditorOpener { CUIPlugin.log(e); } } + + /** + * Opens the editor for an external EFS location, selecting the given region. + */ + public static void openExternalFile(IWorkbenchPage page, URI locationURI, IRegion region, long timestamp, ICElement context) { + IEditorPart editor= null; + try { + editor= EditorUtility.openInEditor(locationURI, context); + if (timestamp == 0) { + try { + timestamp= EFS.getStore(locationURI).fetchInfo().getLastModified(); + } catch (CoreException e) { + CUIPlugin.log(e); + } + } + selectRegion(locationURI, region, timestamp, editor); + } catch (PartInitException e) { + CUIPlugin.log(e); + } + } /** * Opens the editor for an ICElement, selecting the id.