mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Bug 245861 - [semantic highlighting] parser errors during editing leads to incorrect highlighting
This commit is contained in:
parent
22785eee86
commit
81fe52b68d
6 changed files with 110 additions and 15 deletions
|
@ -35,7 +35,8 @@ Require-Bundle: org.eclipse.jface.text,
|
|||
org.eclipse.core.expressions,
|
||||
org.eclipse.cdt.make.core,
|
||||
com.ibm.icu,
|
||||
org.eclipse.ltk.core.refactoring;bundle-version="3.4.0"
|
||||
org.eclipse.ltk.core.refactoring;bundle-version="3.4.0",
|
||||
org.eclipse.core.filesystem;bundle-version="1.2.0"
|
||||
Bundle-ActivationPolicy: lazy
|
||||
Bundle-Vendor: Eclipse.org
|
||||
Bundle-RequiredExecutionEnvironment: J2SE-1.5
|
||||
|
|
|
@ -16,8 +16,11 @@ import java.io.File;
|
|||
import junit.framework.Test;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
import org.eclipse.core.filesystem.EFS;
|
||||
import org.eclipse.core.filesystem.URIUtil;
|
||||
import org.eclipse.core.resources.IFile;
|
||||
import org.eclipse.core.resources.IProject;
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
import org.eclipse.core.runtime.NullProgressMonitor;
|
||||
import org.eclipse.jface.text.DocumentEvent;
|
||||
import org.eclipse.jface.text.IDocument;
|
||||
|
@ -31,7 +34,10 @@ import org.eclipse.swt.graphics.Color;
|
|||
import org.eclipse.swt.graphics.RGB;
|
||||
import org.eclipse.swt.widgets.Event;
|
||||
import org.eclipse.ui.IEditorPart;
|
||||
import org.eclipse.ui.IWorkbenchPage;
|
||||
import org.eclipse.ui.PartInitException;
|
||||
import org.eclipse.ui.PlatformUI;
|
||||
import org.eclipse.ui.ide.IDE;
|
||||
|
||||
import org.eclipse.cdt.core.CCorePlugin;
|
||||
import org.eclipse.cdt.core.model.CModelException;
|
||||
|
@ -124,6 +130,19 @@ public class BasicCEditorTest extends BaseUITestCase {
|
|||
assertNotNull(fDocument);
|
||||
}
|
||||
|
||||
private void setUpEditorUsingFileStore(File file) throws CoreException {
|
||||
final IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
|
||||
IEditorPart editor= IDE.openEditorOnFileStore(activePage, EFS.getStore(URIUtil.toURI(file.getAbsolutePath())));
|
||||
assertNotNull(editor);
|
||||
assertTrue(editor instanceof CEditor);
|
||||
fEditor= (CEditor) editor;
|
||||
fTextWidget= fEditor.getViewer().getTextWidget();
|
||||
assertNotNull(fTextWidget);
|
||||
fAccessor= new Accessor(fTextWidget, StyledText.class);
|
||||
fDocument= fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
|
||||
assertNotNull(fDocument);
|
||||
}
|
||||
|
||||
public void testEditTranslationUnit() throws Exception {
|
||||
final String file= "/ceditor/src/main.cpp";
|
||||
fCProject= EditorTestHelper.createCProject("ceditor", "resources/ceditor", false, false);
|
||||
|
@ -302,6 +321,47 @@ public class BasicCEditorTest extends BaseUITestCase {
|
|||
tmpFile.delete();
|
||||
}
|
||||
|
||||
public void testEditExternalTranslationUnitUsingFileStore() throws Exception {
|
||||
final String file= "/ceditor/src/main.cpp";
|
||||
fCProject= EditorTestHelper.createCProject("ceditor", "resources/ceditor", false, false);
|
||||
IFile mainFile= ResourceTestHelper.findFile(file);
|
||||
assertNotNull(mainFile);
|
||||
File tmpFile= File.createTempFile("tmp", ".cpp");
|
||||
tmpFile.deleteOnExit();
|
||||
FileTool.copy(mainFile.getLocation().toFile(), tmpFile);
|
||||
setUpEditorUsingFileStore(tmpFile);
|
||||
fSourceViewer= EditorTestHelper.getSourceViewer(fEditor);
|
||||
assertTrue(EditorTestHelper.joinReconciler(fSourceViewer, 0, 10000, 100));
|
||||
String content= fDocument.get();
|
||||
setCaret(0);
|
||||
String newtext= "/* "+getName()+" */";
|
||||
type(newtext);
|
||||
type('\n');
|
||||
String newContent= fDocument.get();
|
||||
assertEquals("Edit failed", newtext, newContent.substring(0, newtext.length()));
|
||||
// save
|
||||
fEditor.doSave(new NullProgressMonitor());
|
||||
assertFalse("Editor is still dirty", fEditor.isDirty());
|
||||
// close and reopen
|
||||
EditorTestHelper.closeEditor(fEditor);
|
||||
setUpEditor(tmpFile);
|
||||
fSourceViewer= EditorTestHelper.getSourceViewer(fEditor);
|
||||
assertTrue(EditorTestHelper.joinReconciler(fSourceViewer, 0, 10000, 100));
|
||||
content= fDocument.get();
|
||||
assertEquals("Save failed", newContent, content);
|
||||
// check reconciler
|
||||
ITranslationUnit tUnit= (ITranslationUnit)fEditor.getInputCElement();
|
||||
ICElement[] children= tUnit.getChildren();
|
||||
assertEquals(2, children.length);
|
||||
setCaret(content.length());
|
||||
type('\n');
|
||||
type("void func() {}\n");
|
||||
assertTrue(EditorTestHelper.joinReconciler(fSourceViewer, 0, 10000, 100));
|
||||
children= tUnit.getChildren();
|
||||
assertEquals(3, children.length);
|
||||
tmpFile.delete();
|
||||
}
|
||||
|
||||
public void testSyntaxHighlighting_Bug180433() throws Exception {
|
||||
CColorManager colorMgr= CUIPlugin.getDefault().getTextTools().getColorManager();
|
||||
colorMgr.unbindColor(ICColorConstants.PP_DIRECTIVE);
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
*******************************************************************************/
|
||||
package org.eclipse.cdt.internal.ui.editor;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
@ -51,6 +52,7 @@ import org.eclipse.ui.IFileEditorInput;
|
|||
import org.eclipse.ui.IStorageEditorInput;
|
||||
import org.eclipse.ui.editors.text.ForwardingDocumentProvider;
|
||||
import org.eclipse.ui.editors.text.ILocationProvider;
|
||||
import org.eclipse.ui.editors.text.ILocationProviderExtension;
|
||||
import org.eclipse.ui.editors.text.TextFileDocumentProvider;
|
||||
import org.eclipse.ui.texteditor.IDocumentProvider;
|
||||
import org.eclipse.ui.texteditor.IMarkerUpdater;
|
||||
|
@ -830,7 +832,11 @@ public class CDocumentProvider extends TextFileDocumentProvider {
|
|||
} else if (element instanceof IAdaptable) {
|
||||
IAdaptable adaptable= (IAdaptable)element;
|
||||
ILocationProvider locationProvider= (ILocationProvider)adaptable.getAdapter(ILocationProvider.class);
|
||||
if (locationProvider != null) {
|
||||
if (locationProvider instanceof ILocationProviderExtension) {
|
||||
URI uri= ((ILocationProviderExtension)locationProvider).getURI(element);
|
||||
original= createTranslationUnit(uri);
|
||||
}
|
||||
if (original == null && locationProvider != null) {
|
||||
IPath location= locationProvider.getPath(element);
|
||||
original= createTranslationUnit(location);
|
||||
}
|
||||
|
@ -889,6 +895,22 @@ public class CDocumentProvider extends TextFileDocumentProvider {
|
|||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to synthesize an ITranslationUnit out of thin air.
|
||||
* @param uri the URU of the file in question
|
||||
* @return a translation unit or <code>null</code>
|
||||
*/
|
||||
private ITranslationUnit createTranslationUnit(URI uri) {
|
||||
if (uri == null) {
|
||||
return null;
|
||||
}
|
||||
IEditorInput input= EditorUtility.getEditorInputForLocation(uri, null);
|
||||
if (input instanceof ITranslationUnitEditorInput) {
|
||||
return ((ITranslationUnitEditorInput)input).getTranslationUnit();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/*
|
||||
* @see org.eclipse.ui.editors.text.TextFileDocumentProvider#disposeFileInfo(java.lang.Object,
|
||||
* org.eclipse.ui.editors.text.TextFileDocumentProvider.FileInfo)
|
||||
|
|
|
@ -17,8 +17,6 @@ import java.net.URI;
|
|||
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.cdt.core.model.IBuffer;
|
||||
import org.eclipse.cdt.core.model.IOpenable;
|
||||
import org.eclipse.cdt.core.model.ITranslationUnit;
|
||||
|
@ -53,12 +51,6 @@ public class CustomBufferFactory implements IBufferFactory {
|
|||
return adapter;
|
||||
}
|
||||
|
||||
// external file
|
||||
IPath location= original.getLocation();
|
||||
if (location != null) {
|
||||
return new DocumentAdapter(owner, location);
|
||||
}
|
||||
|
||||
// URI
|
||||
URI locationUri= original.getLocationURI();
|
||||
if (locationUri != null) {
|
||||
|
|
|
@ -1,24 +1,26 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2002, 2006 QNX Software Systems and others.
|
||||
* Copyright (c) 2002, 2008 QNX Software Systems 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
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* QNX Software Systems - Initial API and implementation
|
||||
* QNX Software Systems - Initial API and implementation
|
||||
*******************************************************************************/
|
||||
|
||||
package org.eclipse.cdt.internal.ui.editor;
|
||||
|
||||
import org.eclipse.cdt.core.model.ITranslationUnit;
|
||||
import org.eclipse.ui.IStorageEditorInput;
|
||||
import org.eclipse.ui.editors.text.ILocationProvider;
|
||||
import org.eclipse.ui.editors.text.ILocationProviderExtension;
|
||||
|
||||
import org.eclipse.cdt.core.model.ITranslationUnit;
|
||||
|
||||
/**
|
||||
* ITranslationUnitEditorInput
|
||||
*/
|
||||
public interface ITranslationUnitEditorInput extends IStorageEditorInput, ILocationProvider {
|
||||
public interface ITranslationUnitEditorInput extends IStorageEditorInput, ILocationProvider, ILocationProviderExtension {
|
||||
|
||||
ITranslationUnit getTranslationUnit();
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2000, 2007 QNX Software Systems and others.
|
||||
* Copyright (c) 2000, 2008 QNX Software Systems 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
|
||||
|
@ -12,6 +12,9 @@
|
|||
package org.eclipse.cdt.internal.ui.util;
|
||||
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.eclipse.core.filesystem.URIUtil;
|
||||
import org.eclipse.core.resources.IResource;
|
||||
import org.eclipse.core.resources.IStorage;
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
|
@ -26,6 +29,7 @@ import org.eclipse.ui.PlatformUI;
|
|||
import org.eclipse.ui.editors.text.ILocationProvider;
|
||||
|
||||
import org.eclipse.cdt.core.model.ITranslationUnit;
|
||||
import org.eclipse.cdt.core.resources.EFSFileStorage;
|
||||
|
||||
import org.eclipse.cdt.internal.ui.editor.ITranslationUnitEditorInput;
|
||||
|
||||
|
@ -172,5 +176,19 @@ public class ExternalEditorInput implements ITranslationUnitEditorInput, IPersis
|
|||
public void saveState(IMemento memento) {
|
||||
ExternalEditorInputFactory.saveState(memento, this);
|
||||
}
|
||||
|
||||
/*
|
||||
* @see org.eclipse.ui.editors.text.ILocationProviderExtension#getURI(java.lang.Object)
|
||||
*/
|
||||
public URI getURI(Object element) {
|
||||
if (externalFile instanceof EFSFileStorage) {
|
||||
return ((EFSFileStorage) externalFile).getLocationURI();
|
||||
}
|
||||
IPath location = getPath(element);
|
||||
if (location != null) {
|
||||
return URIUtil.toURI(location);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue