1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-09-03 05:33:33 +02:00

Fix Compare Viewer highlighting

This commit is contained in:
Anton Leherbauer 2006-12-15 10:39:34 +00:00
parent 2631135c07
commit c1ecf194d1
2 changed files with 73 additions and 36 deletions

View file

@ -27,6 +27,7 @@ import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.texteditor.AbstractTextEditor; import org.eclipse.ui.texteditor.AbstractTextEditor;
import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.text.ICPartitions;
import org.eclipse.cdt.internal.ui.text.CSourceViewerConfiguration; import org.eclipse.cdt.internal.ui.text.CSourceViewerConfiguration;
import org.eclipse.cdt.internal.ui.text.CTextTools; import org.eclipse.cdt.internal.ui.text.CTextTools;
@ -130,7 +131,7 @@ public class CMergeViewer extends TextMergeViewer {
if (fSourceViewerConfiguration == null) { if (fSourceViewerConfiguration == null) {
CTextTools tools= CUIPlugin.getDefault().getTextTools(); CTextTools tools= CUIPlugin.getDefault().getTextTools();
IPreferenceStore store = getPreferenceStore(); IPreferenceStore store = getPreferenceStore();
fSourceViewerConfiguration = new CSourceViewerConfiguration(tools.getColorManager(), store, null, null); fSourceViewerConfiguration = new CSourceViewerConfiguration(tools.getColorManager(), store, null, ICPartitions.C_PARTITIONING);
} }
return fSourceViewerConfiguration; return fSourceViewerConfiguration;
} }
@ -139,6 +140,16 @@ public class CMergeViewer extends TextMergeViewer {
return CUIPlugin.getResourceString(TITLE); return CUIPlugin.getResourceString(TITLE);
} }
/*
* @see org.eclipse.compare.contentmergeviewer.TextMergeViewer#getDocumentPartitioning()
*/
protected String getDocumentPartitioning() {
return ICPartitions.C_PARTITIONING;
}
/*
* @see org.eclipse.compare.contentmergeviewer.TextMergeViewer#getDocumentPartitioner()
*/
protected IDocumentPartitioner getDocumentPartitioner() { protected IDocumentPartitioner getDocumentPartitioner() {
return CUIPlugin.getDefault().getTextTools().createDocumentPartitioner(); return CUIPlugin.getDefault().getTextTools().createDocumentPartitioner();
} }

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2005 IBM Corporation and others. * Copyright (c) 2005, 2006 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -8,6 +8,7 @@
* Contributors: * Contributors:
* IBM Corporation - initial API and implementation * IBM Corporation - initial API and implementation
* QNX Software System * QNX Software System
* Anton Leherbauer (Wind River Systems)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.ui.compare; package org.eclipse.cdt.internal.ui.compare;
@ -16,6 +17,19 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import org.eclipse.compare.CompareUI;
import org.eclipse.compare.IEditableContent;
import org.eclipse.compare.IEncodedStreamContentAccessor;
import org.eclipse.compare.IStreamContentAccessor;
import org.eclipse.compare.structuremergeviewer.Differencer;
import org.eclipse.compare.structuremergeviewer.IDiffContainer;
import org.eclipse.compare.structuremergeviewer.IStructureComparator;
import org.eclipse.compare.structuremergeviewer.IStructureCreator;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.parser.CodeReader; import org.eclipse.cdt.core.parser.CodeReader;
import org.eclipse.cdt.core.parser.IParser; import org.eclipse.cdt.core.parser.IParser;
@ -28,15 +42,8 @@ import org.eclipse.cdt.core.parser.ParserMode;
import org.eclipse.cdt.core.parser.ParserUtil; import org.eclipse.cdt.core.parser.ParserUtil;
import org.eclipse.cdt.core.parser.ScannerInfo; import org.eclipse.cdt.core.parser.ScannerInfo;
import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.compare.IEditableContent;
import org.eclipse.compare.IStreamContentAccessor; import org.eclipse.cdt.internal.ui.editor.CDocumentSetupParticipant;
import org.eclipse.compare.structuremergeviewer.Differencer;
import org.eclipse.compare.structuremergeviewer.IDiffContainer;
import org.eclipse.compare.structuremergeviewer.IStructureComparator;
import org.eclipse.compare.structuremergeviewer.IStructureCreator;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
/** /**
* *
*/ */
@ -53,19 +60,23 @@ public class CStructureCreator implements IStructureCreator {
public IStructureComparator getStructure(Object input) { public IStructureComparator getStructure(Object input) {
String s = null; IDocument doc= CompareUI.getDocument(input);
if (input instanceof IStreamContentAccessor) { if (doc == null) {
try { if (input instanceof IStreamContentAccessor) {
s = readString(((IStreamContentAccessor) input).getContents()); String s = null;
} catch (CoreException ex) { try {
s = readString((IStreamContentAccessor) input);
} catch (CoreException ex) {
}
if (s != null) {
doc = new Document(s);
new CDocumentSetupParticipant().setup(doc);
}
} }
} }
if (doc == null) {
if (s == null) { return null;
s = new String();
} }
Document doc = new Document(s);
CNode root = new CNode(null, ICElement.C_UNIT, "root", doc, 0, 0); //$NON-NLS-1$ CNode root = new CNode(null, ICElement.C_UNIT, "root", doc, 0, 0); //$NON-NLS-1$
ISourceElementRequestor builder = new CParseTreeBuilder(root, doc); ISourceElementRequestor builder = new CParseTreeBuilder(root, doc);
@ -74,7 +85,7 @@ public class CStructureCreator implements IStructureCreator {
//are bugs while parsing C files, we might want to create a separate Structure //are bugs while parsing C files, we might want to create a separate Structure
//compare for c files, but we'll never be completely right about .h files //compare for c files, but we'll never be completely right about .h files
IScanner scanner = IScanner scanner =
ParserFactory.createScanner(new CodeReader(s.toCharArray()), new ScannerInfo(), ParserMode.QUICK_PARSE, ParserLanguage.CPP, builder, new NullLogService(), null); ParserFactory.createScanner(new CodeReader(doc.get().toCharArray()), new ScannerInfo(), ParserMode.QUICK_PARSE, ParserLanguage.CPP, builder, new NullLogService(), null);
IParser parser = ParserFactory.createParser(scanner, builder, ParserMode.QUICK_PARSE, ParserLanguage.CPP, ParserUtil.getParserLogService() ); IParser parser = ParserFactory.createParser(scanner, builder, ParserMode.QUICK_PARSE, ParserLanguage.CPP, ParserUtil.getParserLogService() );
parser.parse(); parser.parse();
} catch (Exception e) { } catch (Exception e) {
@ -122,41 +133,56 @@ public class CStructureCreator implements IStructureCreator {
if (node instanceof IStreamContentAccessor) { if (node instanceof IStreamContentAccessor) {
IStreamContentAccessor sca = (IStreamContentAccessor) node; IStreamContentAccessor sca = (IStreamContentAccessor) node;
try { try {
return readString(sca.getContents()); return readString(sca);
} catch (CoreException ex) { } catch (CoreException ex) {
} }
} }
return null; return null;
} }
/** private static String readString(IStreamContentAccessor sa) throws CoreException {
* Returns null if an error occurred. InputStream is= sa.getContents();
*/ if (is != null) {
private static String readString(InputStream is) { String encoding= null;
if (sa instanceof IEncodedStreamContentAccessor) {
try {
encoding= ((IEncodedStreamContentAccessor) sa).getCharset();
} catch (Exception e) {
}
}
if (encoding == null)
encoding= ResourcesPlugin.getEncoding();
return readString(is, encoding);
}
return null;
}
private static String readString(InputStream is, String encoding) {
if (is == null) if (is == null)
return null; return null;
BufferedReader reader = null; BufferedReader reader= null;
try { try {
StringBuffer buffer = new StringBuffer(); StringBuffer buffer= new StringBuffer();
char[] part = new char[2048]; char[] part= new char[2048];
int read = 0; int read= 0;
reader = new BufferedReader(new InputStreamReader(is)); reader= new BufferedReader(new InputStreamReader(is, encoding));
while ((read = reader.read(part)) != -1) while ((read= reader.read(part)) != -1)
buffer.append(part, 0, read); buffer.append(part, 0, read);
return buffer.toString(); return buffer.toString();
} catch (IOException ex) { } catch (IOException ex) {
// NeedWork
} finally { } finally {
if (reader != null) { if (reader != null) {
try { try {
reader.close(); reader.close();
} catch (IOException ex) { } catch (IOException ex) {
// silently ignored
} }
} }
} }
return null; return null;
} }
} }