1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 14:42:11 +02:00

Position trackers for non-local external files, bug 261912.

This commit is contained in:
Markus Schorn 2009-02-02 16:27:44 +00:00
parent 251635be7d
commit fb5c539efc
3 changed files with 106 additions and 26 deletions

View file

@ -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.
*
* <p> This interface is not intended to be implemented by clients. </p>
*
* <p>
* <strong>EXPERIMENTAL</strong>. 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.
* </p>
* @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 <code>null</code>.
*/
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. <p>
* 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 <code>null</code>.
* @since 5.1
*/
public IPositionConverter findPositionConverter(URI externalLocation, long timestamp);
}

View file

@ -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<IPath, PositionTrackerChain> 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<Object, PositionTrackerChain> fPositionTrackerMap;
private PositionTrackerManager() {
fPositionTrackerMap= new HashMap<IPath, PositionTrackerChain>();
fPositionTrackerMap= new HashMap<Object, PositionTrackerChain>();
}
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;
}
}

View file

@ -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.