1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-23 17:05:26 +02:00

Move the MakefileDocumentProvider to use Eclipse-3.0

TextFileBuffer management this will allow us to open
external files also
This commit is contained in:
Alain Magloire 2004-08-22 22:18:44 +00:00
parent 1ffaa3cf23
commit 9bf3b2ef78
8 changed files with 372 additions and 133 deletions

View file

@ -423,6 +423,8 @@
</action>
</actionSet>
</extension>
<!-- Makefile Editor extensions. -->
<extension
id="org.eclipse.cdt.make.editor"
name="MakefileEditor"
@ -436,6 +438,26 @@
id="org.eclipse.cdt.make.editor">
</editor>
</extension>
<extension
id="org.eclipse.cdt.make.ui.MakefileDocumentFactory"
name="%MakefileDocumentFactory.name"
point="org.eclipse.core.filebuffers.documentCreation">
<factory
extensions="*"
class="org.eclipse.cdt.make.internal.ui.editor.MakefileDocumentFactory">
</factory>
</extension>
<extension
id="org.eclipse.cdt.make.ui.MakefileDocumentSetupParticipant"
name="%MakefileDocumentSetupParticipant.name"
point="org.eclipse.core.filebuffers.documentSetup">
<participant
extensions="*"
class="org.eclipse.cdt.make.internal.ui.editor.MakefileDocumentSetupParticipant">
</participant>
</extension>
<extension
point="org.eclipse.ui.startup">
<startup

View file

@ -0,0 +1,30 @@
/**********************************************************************
* Copyright (c) 2002,2003,2004 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* QNX Software Systems - Initial API and implementation
***********************************************************************/
package org.eclipse.cdt.make.internal.ui.editor;
import org.eclipse.jface.text.IDocument;
/**
* MakefileDocumentFactory
*/
public class MakefileDocumentFactory {
public MakefileDocumentFactory() {
}
/*
* @see org.eclipse.core.filebuffers.IDocumentFactory#createDocument()
*/
public IDocument createDocument() {
return new PartiallySynchronizedDocument();
}
}

View file

@ -10,168 +10,157 @@
*******************************************************************************/
package org.eclipse.cdt.make.internal.ui.editor;
import java.io.IOException;
import java.io.StringReader;
import java.util.Iterator;
import org.eclipse.cdt.make.core.MakeCorePlugin;
import org.eclipse.cdt.make.core.makefile.IMakefile;
import org.eclipse.cdt.make.internal.ui.MakeUIPlugin;
import org.eclipse.cdt.make.internal.ui.text.makefile.MakefilePartitionScanner;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentPartitioner;
import org.eclipse.jface.text.rules.DefaultPartitioner;
import org.eclipse.jface.text.ISynchronizable;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.editors.text.FileDocumentProvider;
import org.eclipse.ui.editors.text.ForwardingDocumentProvider;
import org.eclipse.ui.editors.text.TextFileDocumentProvider;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ResourceMarkerAnnotationModel;
/**
*/
public class MakefileDocumentProvider extends FileDocumentProvider implements IMakefileDocumentProvider {
public class MakefileDocumentProvider extends TextFileDocumentProvider implements IMakefileDocumentProvider {
IMakefile fMakefile;
private static MakefilePartitionScanner scanner = null;
/**
* Bundle of all required informations to allow working copy management.
*/
protected class MakefileInfo extends FileInfo {
IMakefile fCopy;
public MakefileInfo(IDocument document, IAnnotationModel model, FileSynchronizer fileSynchronizer, IMakefile copy) {
super(document, model, fileSynchronizer);
fCopy = copy;
protected class MakefileAnnotationModel extends ResourceMarkerAnnotationModel /*implements IProblemRequestor */{
/**
* @param resource
*/
public MakefileAnnotationModel(IResource resource) {
super(resource);
}
public void setModificationStamp(long timeStamp) {
fModificationStamp = timeStamp;
/**
* @param makefile
*/
public void setMakefile(IMakefile makefile) {
fMakefile = makefile;
}
}
/**
* Constructor for MakefileDocumentProvider.
* Remembers a IMakefile for each element.
*/
protected class MakefileFileInfo extends FileInfo {
public IMakefile fCopy;
}
public MakefileDocumentProvider() {
super();
IDocumentProvider provider= new TextFileDocumentProvider(new MakefileStorageDocumentProvider());
provider= new ForwardingDocumentProvider(MakefileDocumentSetupParticipant.MAKEFILE_PARTITIONING, new MakefileDocumentSetupParticipant(), provider);
setParentDocumentProvider(provider);
}
/**
* @see org.eclipse.ui.texteditor.AbstractDocumentProvider#createDocument(Object)
*/
protected IDocument createDocument(Object element) throws CoreException {
IDocument document = super.createDocument(element);
if (document != null) {
IDocumentPartitioner partitioner = createPartitioner();
partitioner.connect(document);
document.setDocumentPartitioner(partitioner);
}
return document;
}
private IDocumentPartitioner createPartitioner() {
return new DefaultPartitioner(getPartitionScanner(), MakefilePartitionScanner.TYPES);
}
private MakefilePartitionScanner getPartitionScanner() {
if (scanner == null)
scanner = new MakefilePartitionScanner();
return scanner;
}
/*
* @see AbstractDocumentProvider#createElementInfo(Object)
*/
protected ElementInfo createElementInfo(Object element) throws CoreException {
if (element instanceof IFileEditorInput) {
IFileEditorInput input = (IFileEditorInput) element;
IMakefile makefile = createMakefile(input.getFile());
if (makefile == null) {
return super.createElementInfo(element);
}
try {
refreshFile(input.getFile());
} catch (CoreException x) {
handleCoreException(x, MakeUIPlugin.getResourceString("MakeDocumentProvider.exception.createElementInfo")); //$NON-NLS-1$
}
IDocument d = null;
IStatus s = null;
try {
d = createDocument(element);
} catch (CoreException x) {
s = x.getStatus();
d = createEmptyDocument();
}
IAnnotationModel m = createAnnotationModel(element);
FileSynchronizer f = new FileSynchronizer(input);
f.install();
FileInfo info = new MakefileInfo(d, m, f, makefile);
info.fModificationStamp = computeModificationStamp(input.getFile());
info.fStatus = s;
info.fEncoding = getPersistedEncoding(input);
return info;
}
return super.createElementInfo(element);
}
/**
* @param file
* @return
*/
private IMakefile createMakefile(IFile file) {
return MakeCorePlugin.getDefault().createMakefile(file);
}
/*
* @see AbstractDocumentProvider#doSaveDocument(IProgressMonitor, Object, IDocument, boolean)
*/
protected void doSaveDocument(IProgressMonitor monitor, Object element, IDocument document, boolean overwrite) throws CoreException {
// Update the makefile directives tree;
ElementInfo elementInfo= getElementInfo(element);
if (elementInfo instanceof MakefileInfo) {
MakefileInfo info= (MakefileInfo) elementInfo;
String content = document.get();
StringReader reader = new StringReader(content);
try {
info.fCopy.parse(info.fCopy.getFileName(), reader);
} catch (IOException e) {
}
}
super.doSaveDocument(monitor, element, document, overwrite);
}
/* (non-Javadoc)
* @see org.eclipse.ui.editors.text.TextFileDocumentProvider#createAnnotationModel(org.eclipse.core.resources.IFile)
*/
protected IAnnotationModel createAnnotationModel(IFile file) {
return new MakefileAnnotationModel(file);
}
/* (non-Javadoc)
* @see org.eclipse.cdt.make.internal.ui.editor.IMakefileDocumentProvider#shutdown()
/*
* @see org.eclipse.ui.editors.text.TextFileDocumentProvider#createFileInfo(java.lang.Object)
*/
public void shutdown() {
Iterator e = getConnectedElements();
while (e.hasNext()) {
disconnect(e.next());
}
}
protected FileInfo createFileInfo(Object element) throws CoreException {
if (!(element instanceof IFileEditorInput))
return null;
IFileEditorInput input= (IFileEditorInput) element;
IMakefile original= createMakefile(input.getFile());
if (original == null)
return null;
FileInfo info= super.createFileInfo(element);
if (!(info instanceof MakefileFileInfo)) {
return null;
}
MakefileFileInfo makefileInfo= (MakefileFileInfo) info;
setUpSynchronization(makefileInfo);
// IProblemRequestor requestor= cuInfo.fModel instanceof IProblemRequestor ? (IProblemRequestor) cuInfo.fModel : null;
//original.becomeWorkingCopy(requestor, getProgressMonitor());
makefileInfo.fCopy = original;
if (makefileInfo.fModel instanceof MakefileAnnotationModel) {
MakefileAnnotationModel model= (MakefileAnnotationModel) makefileInfo.fModel;
model.setMakefile(makefileInfo.fCopy);
}
// if (requestor instanceof IProblemRequestorExtension) {
// IProblemRequestorExtension extension= (IProblemRequestorExtension) requestor;
// extension.setIsActive(isHandlingTemporaryProblems());
// }
return makefileInfo;
}
/* (non-Javadoc)
* @see org.eclipse.ui.editors.text.TextFileDocumentProvider#disposeFileInfo(java.lang.Object, org.eclipse.ui.editors.text.TextFileDocumentProvider.FileInfo)
*/
protected void disposeFileInfo(Object element, FileInfo info) {
if (info instanceof MakefileFileInfo) {
MakefileFileInfo makefileInfo= (MakefileFileInfo) info;
if (makefileInfo.fCopy != null) {
//makefileInfo.fCopy.dispose();
makefileInfo.fCopy = null;
}
}
super.disposeFileInfo(element, info);
}
/*
* @see org.eclipse.ui.editors.text.TextFileDocumentProvider#createEmptyFileInfo()
*/
protected FileInfo createEmptyFileInfo() {
return new MakefileFileInfo();
}
/*
* @see org.eclipse.cdt.make.internal.ui.editor.ICompilationUnitDocumentProvider#getWorkingCopy(java.lang.Object)
* @see org.eclipse.cdt.make.internal.ui.IMakefileDocumentProvider#getWorkingCopy(java.lang.Object)
*/
public IMakefile getWorkingCopy(Object element) {
ElementInfo elementInfo = getElementInfo(element);
if (elementInfo instanceof MakefileInfo) {
MakefileInfo info = (MakefileInfo) elementInfo;
FileInfo fileInfo= getFileInfo(element);
if (fileInfo instanceof MakefileFileInfo) {
MakefileFileInfo info= (MakefileFileInfo) fileInfo;
return info.fCopy;
}
return null;
}
/*
* @see org.eclipse.cdt.make.internal.ui.IMakefileDocumentProvider#shutdown()
*/
public void shutdown() {
Iterator e= getConnectedElementsIterator();
while (e.hasNext())
disconnect(e.next());
}
private void setUpSynchronization(MakefileFileInfo antInfo) {
IDocument document= antInfo.fTextFileBuffer.getDocument();
IAnnotationModel model= antInfo.fModel;
if (document instanceof ISynchronizable && model instanceof ISynchronizable) {
Object lock= ((ISynchronizable) document).getLockObject();
((ISynchronizable) model).setLockObject(lock);
}
}
}

View file

@ -0,0 +1,51 @@
/**********************************************************************
* Copyright (c) 2002,2003,2004 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* QNX Software Systems - Initial API and implementation
***********************************************************************/
package org.eclipse.cdt.make.internal.ui.editor;
import org.eclipse.cdt.make.internal.ui.text.makefile.MakefilePartitionScanner;
import org.eclipse.core.filebuffers.IDocumentSetupParticipant;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentExtension3;
import org.eclipse.jface.text.IDocumentPartitioner;
import org.eclipse.jface.text.rules.DefaultPartitioner;
/**
* MakefileDocumentSetupParticipant
* The document setup participant for Ant.
*/
public class MakefileDocumentSetupParticipant implements IDocumentSetupParticipant {
/**
* The name of the Makefiile partitioning.
*/
public final static String MAKEFILE_PARTITIONING= "___makefile_partitioning"; //$NON-NLS-1$
public MakefileDocumentSetupParticipant() {
}
/*
* @see org.eclipse.core.filebuffers.IDocumentSetupParticipant#setup(org.eclipse.jface.text.IDocument)
*/
public void setup(IDocument document) {
if (document instanceof IDocumentExtension3) {
IDocumentExtension3 extension3= (IDocumentExtension3) document;
IDocumentPartitioner partitioner = createDocumentPartitioner();
extension3.setDocumentPartitioner(MAKEFILE_PARTITIONING, partitioner);
partitioner.connect(document);
}
}
private IDocumentPartitioner createDocumentPartitioner() {
return new DefaultPartitioner(
new MakefilePartitionScanner(), MakefilePartitionScanner.TYPES);
}
}

View file

@ -0,0 +1,48 @@
/**********************************************************************
* Copyright (c) 2002,2003,2004 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* QNX Software Systems - Initial API and implementation
***********************************************************************/
package org.eclipse.cdt.make.internal.ui.editor;
import org.eclipse.cdt.make.internal.ui.text.makefile.MakefilePartitionScanner;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentExtension3;
import org.eclipse.jface.text.IDocumentPartitioner;
import org.eclipse.jface.text.rules.DefaultPartitioner;
import org.eclipse.ui.editors.text.StorageDocumentProvider;
/**
* MakefileStorageDocumentProvider
*/
public class MakefileStorageDocumentProvider extends StorageDocumentProvider {
/*
* @see org.eclipse.ui.editors.text.StorageDocumentProvider#setupDocument(java.lang.Object,
* org.eclipse.jface.text.IDocument)
*/
protected void setupDocument(Object element, IDocument document) {
if (document != null) {
IDocumentPartitioner partitioner= createDocumentPartitioner();
if (document instanceof IDocumentExtension3) {
IDocumentExtension3 extension3= (IDocumentExtension3) document;
extension3.setDocumentPartitioner(MakefileDocumentSetupParticipant.MAKEFILE_PARTITIONING, partitioner);
} else {
document.setDocumentPartitioner(partitioner);
}
partitioner.connect(document);
}
}
private IDocumentPartitioner createDocumentPartitioner() {
return new DefaultPartitioner(
new MakefilePartitionScanner(), MakefilePartitionScanner.TYPES);
}
}

View file

@ -0,0 +1,97 @@
/**********************************************************************
* Copyright (c) 2002,2003,2004 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
***********************************************************************/
package org.eclipse.cdt.make.internal.ui.editor;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.ISynchronizable;
/**
* PartiallySynchronizedDocument
* Document that can also be used by a background reconciler.
*/
public class PartiallySynchronizedDocument extends Document implements ISynchronizable {
private final Object fInternalLockObject= new Object();
private Object fLockObject;
/* (non-Javadoc)
* @see org.eclipse.jface.text.IDocumentExtension#startSequentialRewrite(boolean)
*/
synchronized public void startSequentialRewrite(boolean normalized) {
super.startSequentialRewrite(normalized);
}
/* (non-Javadoc)
* @see org.eclipse.jface.text.IDocumentExtension#stopSequentialRewrite()
*/
synchronized public void stopSequentialRewrite() {
super.stopSequentialRewrite();
}
/* (non-Javadoc)
* @see org.eclipse.jface.text.IDocument#get()
*/
synchronized public String get() {
return super.get();
}
/* (non-Javadoc)
* @see org.eclipse.jface.text.IDocument#get(int, int)
*/
synchronized public String get(int offset, int length) throws BadLocationException {
return super.get(offset, length);
}
/* (non-Javadoc)
* @see org.eclipse.jface.text.IDocument#getChar(int)
*/
synchronized public char getChar(int offset) throws BadLocationException {
return super.getChar(offset);
}
/* (non-Javadoc)
* @see org.eclipse.jface.text.IDocument#replace(int, int, java.lang.String)
*/
synchronized public void replace(int offset, int length, String text) throws BadLocationException {
super.replace(offset, length, text);
//TODO to be used for incremental parsing...not for 3.0
// if (length == 0 && text != null) {
// // Insert
// } else if (text == null || text.length() == 0) {
// // Remove
// } else {
// fAntModel.setReplaceHasOccurred();
// }
}
/* (non-Javadoc)
* @see org.eclipse.jface.text.IDocument#set(java.lang.String)
*/
synchronized public void set(String text) {
super.set(text);
}
/*
* @see org.eclipse.jface.text.ISynchronizable#setLockObject(java.lang.Object)
*/
public void setLockObject(Object lockObject) {
fLockObject= lockObject;
}
/*
* @see org.eclipse.jface.text.ISynchronizable#getLockObject()
*/
public Object getLockObject() {
return fLockObject == null ? fInternalLockObject : fLockObject;
}
}

View file

@ -57,7 +57,7 @@ public class MakefileCodeScanner extends RuleBasedScanner {
List rules = new ArrayList();
// Add rule for single line comments.
rules.add(new EndOfLineRule("#", comment, '\\')); //$NON-NLS-1$
rules.add(new EndOfLineRule("#", comment, '\\', true)); //$NON-NLS-1$
// Add generic whitespace rule.
rules.add(new WhitespaceRule(new IWhitespaceDetector() {

View file

@ -77,13 +77,15 @@ public class MakefileReconcilingStrategy implements IReconcilingStrategy {
private void reconcile() {
IMakefile makefile = fManager.getWorkingCopy(fEditor.getEditorInput());
String content = fDocumentProvider.getDocument(fEditor.getEditorInput()).get();
StringReader reader = new StringReader(content);
try {
if (makefile != null) {
String content = fDocumentProvider.getDocument(fEditor.getEditorInput()).get();
StringReader reader = new StringReader(content);
try {
makefile.parse(makefile.getFileName(), reader);
} catch (IOException e) {
} catch (IOException e) {
}
fOutliner.update();
}
fOutliner.update();
}
}