Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9eb452f0063bf74ef9140715fb4b370d5d5495f9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*******************************************************************************
 * Copyright (c) 2005, 2016 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *     QNX Software System
 *     Anton Leherbauer (Wind River Systems)
 *     Andrew Ferguson (Symbian)
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.compare;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.eclipse.compare.IEncodedStreamContentAccessor;
import org.eclipse.compare.ISharedDocumentAdapter;
import org.eclipse.compare.IStreamContentAccessor;
import org.eclipse.compare.ResourceNode;
import org.eclipse.compare.contentmergeviewer.IDocumentRange;
import org.eclipse.compare.structuremergeviewer.DocumentRangeNode;
import org.eclipse.compare.structuremergeviewer.IStructureComparator;
import org.eclipse.compare.structuremergeviewer.StructureCreator;
import org.eclipse.compare.structuremergeviewer.StructureRootNode;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentPartitioner;
import org.eclipse.jface.text.Position;

import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.gnu.cpp.GPPLanguage;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ILanguage;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.parser.FileContent;
import org.eclipse.cdt.core.parser.IScannerInfo;
import org.eclipse.cdt.core.parser.IncludeFileContentProvider;
import org.eclipse.cdt.core.parser.ParserUtil;
import org.eclipse.cdt.core.parser.ScannerInfo;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.text.ICPartitions;
import org.eclipse.cdt.ui.text.doctools.IDocCommentOwner;

import org.eclipse.cdt.internal.ui.text.doctools.DocCommentOwnerManager;

/**
 * A structure creator for C/C++ translation units.
 */
public class CStructureCreator extends StructureCreator {

	private static final String NAME = "CStructureCreator.name"; //$NON-NLS-1$

	public CStructureCreator() {
	}

	@Override
	public String getName() {
		return CUIPlugin.getResourceString(NAME);
	}

	/*
	 * @see IStructureCreator#getContents
	 */
	@Override
	public String getContents(Object node, boolean ignoreWhitespace) {
		if (node instanceof IDocumentRange) {
			IDocumentRange documentRange= (IDocumentRange)node;
			final Position range = documentRange.getRange();
			try {
				return documentRange.getDocument().get(range.getOffset(), range.getLength());
			} catch (BadLocationException exc) {
			}
		}
		if (node instanceof IStreamContentAccessor) {
			IStreamContentAccessor sca = (IStreamContentAccessor) node;
			try {
				return readString(sca);
			} catch (CoreException ex) {
			}
		}
		return null;
	}

	/*
	 * @see org.eclipse.compare.structuremergeviewer.StructureCreator#createStructureComparator(java.lang.Object, org.eclipse.jface.text.IDocument, org.eclipse.compare.ISharedDocumentAdapter, org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	protected IStructureComparator createStructureComparator(Object element,
			IDocument document, ISharedDocumentAdapter sharedDocumentAdapter,
			IProgressMonitor monitor) throws CoreException {

		DocumentRangeNode root= new StructureRootNode(document, element, this, sharedDocumentAdapter);

		// don't follow inclusions
		IncludeFileContentProvider contentProvider = IncludeFileContentProvider.getEmptyFilesProvider();
		
		// empty scanner info
		IScannerInfo scanInfo= new ScannerInfo();
		
		// determine the language
		boolean isSource[]= {false};
		ILanguage language= determineLanguage(element, isSource);
		
		FileContent content = FileContent.create("<text>", isSource[0], document.get().toCharArray()); //$NON-NLS-1$
		
		try {
			IASTTranslationUnit ast = language.getASTTranslationUnit(content, scanInfo, contentProvider, null,
					0, ParserUtil.getParserLogService());
			CStructureCreatorVisitor structureCreator= new CStructureCreatorVisitor(root);
			// build structure
			ast.accept(structureCreator);
		} catch (CoreException exc) {
			CUIPlugin.log(exc);
		}

		return root;
	}

	/**
	 * Try to determine the <code>ILanguage</code> for the given input element.
	 * 
	 * @param element
	 * @return a language instance
	 */
	private ILanguage determineLanguage(Object element, boolean[] isSource) {
		ILanguage language= null;
		if (element instanceof ResourceNode) {
			IResource resource= ((ResourceNode)element).getResource();
			if (resource.getType() == IResource.FILE) {
				ITranslationUnit tUnit= (ITranslationUnit)CoreModel.getDefault().create(resource);
				if (tUnit != null) {
					try {
						language= tUnit.getLanguage();
						isSource[0]= tUnit.isSourceUnit();
					} catch (CoreException exc) {
						// silently ignored
					}
				}
			}
		}
		if (language == null) {
			language= GPPLanguage.getDefault();
		}
		return language;
	}

	@Override
	protected String getDocumentPartitioning() {
		return ICPartitions.C_PARTITIONING;
	}
	
	@Override
	protected IDocumentPartitioner getDocumentPartitioner() {
		// use workspace default for highlighting doc comments in compare viewer
		IDocCommentOwner owner= DocCommentOwnerManager.getInstance().getWorkspaceCommentOwner();
		return CUIPlugin.getDefault().getTextTools().createDocumentPartitioner(owner);
	}
	
	private static String readString(IStreamContentAccessor sa) throws CoreException {
		InputStream is= sa.getContents();
		if (is != null) {
			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)
			return null;
		BufferedReader reader= null;
		try {
			StringBuilder buffer= new StringBuilder();
			char[] part= new char[2048];
			int read= 0;
			reader= new BufferedReader(new InputStreamReader(is, encoding));

			while ((read= reader.read(part)) != -1)
				buffer.append(part, 0, read);
			
			return buffer.toString();
			
		} catch (IOException ex) {
			// NeedWork
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException ex) {
					// silently ignored
				}
			}
		}
		return null;
	}
}

Back to the top