Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 40ae8359181d6d291ffd8482cfc5cfac4f7572ce (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) 2009, 2015 Wind River Systems, Inc. and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.ui;

import java.net.URI;

import org.eclipse.core.filebuffers.LocationKind;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentExtension3;
import org.eclipse.jface.text.IDocumentPartitioner;
import org.eclipse.jface.text.rules.FastPartitioner;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.texteditor.IDocumentProvider;

import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ISourceReference;
import org.eclipse.cdt.core.model.IWorkingCopy;
import org.eclipse.cdt.ui.text.ICPartitions;
import org.eclipse.cdt.ui.text.IColorManager;

import org.eclipse.cdt.internal.ui.editor.ITranslationUnitEditorInput;
import org.eclipse.cdt.internal.ui.text.asm.AsmPartitionScanner;
import org.eclipse.cdt.internal.ui.util.EditorUtility;

/**
 * This class provides utilities for clients of the CDT UI plug-in.
 * This class provides static methods for:
 * <ul>
 *  <li>opening an editor on a C model element.</li>
 *  <li>accessing working copy manager and document provider used with C model elements.</li>
 *  <li>accessing color manager used for syntax coloring of C/C++ files.</li>
 * </ul>
 *
 * @noinstantiate This class is not intended to be instantiated by clients.
 * @since 5.1
 */
public final class CDTUITools {
	private CDTUITools() {
		// prevent instantiation
	}
	
	/**
	 * Returns the color manager which is used to manage
	 * colors needed for syntax highlighting.
	 *
	 * @return the color manager to be used for C/C++ text viewers
	 */
	public static IColorManager getColorManager() {
		return CUIPlugin.getDefault().getTextTools().getColorManager();
	}

	/**
	 * Opens an editor on the given C model element in the active page. Valid are elements that are {@link ISourceReference}.
	 *
	 * @param element the input element
	 * @return returns the editor part of the opened editor or <code>null</code> if the element is not a {@link ISourceReference} or the
	 * file was opened in an external editor.
	 * @exception PartInitException if the editor could not be initialized or no workbench page is active
	 * @exception CModelException if this element does not exist or if an exception occurs while accessing its underlying resource
	 */
	public static IEditorPart openInEditor(ICElement element) throws CModelException, PartInitException {
		return openInEditor(element, true, true);
	}

	/**
	 * Opens an editor on the given C model element in the active page. Valid are elements that are {@link ISourceReference}.
	 *
	 * @param element the input element
	 * @return returns the editor part of the opened editor or <code>null</code> if the element is not a {@link ISourceReference} or the
	 * file was opened in an external editor.
	 * @exception PartInitException if the editor could not be initialized or no workbench page is active
	 * @exception CModelException if this element does not exist or if an exception occurs while accessing its underlying resource
	 */
	public static IEditorPart openInEditor(ICElement element, boolean activate, boolean reveal) throws CModelException, PartInitException {
		if (!(element instanceof ISourceReference)) {
			return null;
		}
		IEditorPart part= EditorUtility.openInEditor(element, activate);
		if (reveal && part != null) {
			EditorUtility.revealInEditor(part, element);
		}
		return part;
	}

	/**
	 * Reveals the given C model element  in the given editor.. 
	 *
	 * @param part the editor displaying a translation unit
	 * @param element the element to be revealed
	 */
	public static void revealInEditor(IEditorPart part, ICElement element) {
		EditorUtility.revealInEditor(part, element);
	}

	/**
	 * Returns the working copy manager for the CDT UI plug-in.
	 *
	 * @return the working copy manager for the CDT UI plug-in
	 */
	public static IWorkingCopyManager getWorkingCopyManager() {
		return CUIPlugin.getDefault().getWorkingCopyManager();
	}

	/**
	 * Returns the document provider used for C/C++ files.
	 *
	 * @return the document provider for C/C++ files.
	 *
	 * @see IDocumentProvider
	 */
	public static IDocumentProvider getDocumentProvider() {
		return CUIPlugin.getDefault().getDocumentProvider();
	}

	/**
	 * Returns the <code>ICElement</code> element wrapped by the given editor input.
	 *
	 * @param editorInput the editor input
	 * @return the ICElement wrapped by <code>editorInput</code> or <code>null</code> if none
	 */
	public static ICElement getEditorInputCElement(IEditorInput editorInput) {
		if (editorInput instanceof ITranslationUnitEditorInput) {
			return ((ITranslationUnitEditorInput) editorInput).getTranslationUnit();
		}
		IWorkingCopy tu= CUIPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(editorInput);
		if (tu != null)
			return tu;

		return editorInput.getAdapter(ICElement.class);
	}

	/**
	 * Utility method to get an editor input for the given file system location.
	 * If the location denotes a workspace file, a <code>FileEditorInput</code>
	 * is returned, otherwise, the input is an <code>IURIEditorInput</code>
	 * assuming the location points to an existing file in an Eclipse file system.
	 * The <code>ICElement</code> is used to determine the associated project
	 * in case the location can not be resolved to a workspace <code>IFile</code>.
	 *
	 * @param locationURI  a valid Eclipse file system URI
	 * @param context  an element related to the target file, may be <code>null</code>
	 * @return an editor input
	 */
	public static IEditorInput getEditorInputForLocation(URI locationURI, ICElement context) {
		return EditorUtility.getEditorInputForLocation(locationURI, context);
	}

	/**
	 * Utility method to get an editor input for the given file system location.
	 * If the location denotes a workspace file, a <code>FileEditorInput</code>
	 * is returned, otherwise, the input is an <code>IURIEditorInput</code>
	 * assuming the location points to an existing file in the file system.
	 * The <code>ICElement</code> is used to determine the associated project
	 * in case the location can not be resolved to a workspace <code>IFile</code>.
	 *
	 * @param location  a valid file system location
	 * @param context  an element related to the target file, may be <code>null</code>
	 * @return an editor input
	 */
	public static IEditorInput getEditorInputForLocation(IPath location, ICElement context) {
		return EditorUtility.getEditorInputForLocation(location, context);
	}

	/**
	 * Sets up the given document for the default C/C++ partitioning.
	 * 
	 * @param document the document to be set up
	 * @param location the path of the resource backing the document. May be null.
	 * @param locationKind the type of path specified above. May be null.
	 */
	public static void setupCDocument(IDocument document, IPath location, LocationKind locationKind) {
		CUIPlugin.getDefault().getTextTools().setupCDocument(document, location, locationKind);
	}

	/**
	 * Create a document partitioner suitable for Assembly source.
	 */
	public static IDocumentPartitioner createAsmDocumentPartitioner() {
		return new FastPartitioner(new AsmPartitionScanner(), ICPartitions.ALL_ASM_PARTITIONS);
	}

	/**
	 * Sets up the given document for the default Assembly partitioning.
	 * 
	 * @param document the document to be set up
	 */
	public static void setupAsmDocument(IDocument document) {
		IDocumentPartitioner partitioner= createAsmDocumentPartitioner();
		if (document instanceof IDocumentExtension3) {
			IDocumentExtension3 extension3= (IDocumentExtension3) document;
			extension3.setDocumentPartitioner(ICPartitions.C_PARTITIONING, partitioner);
		} else {
			document.setDocumentPartitioner(partitioner);
		}
		partitioner.connect(document);
	}

}

Back to the top