Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0a221ddfe07ac32b2c554f92e21dd8fab009635c (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*******************************************************************************
 * Copyright (c) 2008, 2013 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
 *******************************************************************************/
package org.eclipse.ptp.internal.rdt.ui.editor;

import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.internal.ui.editor.CContentOutlinePage;
import org.eclipse.cdt.internal.ui.editor.CEditor;
import org.eclipse.cdt.internal.ui.editor.CSourceViewer;
import org.eclipse.cdt.internal.ui.editor.SemanticHighlightings;
import org.eclipse.cdt.internal.ui.text.CTextTools;
import org.eclipse.cdt.internal.ui.util.EditorUtility;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.PreferenceConstants;
import org.eclipse.cdt.ui.actions.CdtActionConstants;
import org.eclipse.cdt.ui.text.ICPartitions;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.help.IContextProvider;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.source.ISharedTextColors;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.IVerticalRuler;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.jface.text.source.projection.ProjectionViewer;
import org.eclipse.ptp.internal.rdt.editor.RemoteCEditor;
import org.eclipse.ptp.internal.rdt.ui.RDTHelpContextIds;
import org.eclipse.ptp.internal.rdt.ui.actions.OpenViewActionGroup;
import org.eclipse.ptp.internal.rdt.ui.search.actions.SelectionSearchGroup;
import org.eclipse.ptp.rdt.core.resources.RemoteNature;
import org.eclipse.ptp.rdt.core.serviceproviders.IIndexServiceProvider;
import org.eclipse.ptp.rdt.core.services.IRDTServiceConstants;
import org.eclipse.ptp.rdt.editor.info.IRemoteCEditorInfoProvider;
import org.eclipse.ptp.services.core.IService;
import org.eclipse.ptp.services.core.IServiceConfiguration;
import org.eclipse.ptp.services.core.IServiceModelManager;
import org.eclipse.ptp.services.core.IServiceProvider;
import org.eclipse.ptp.services.core.ServiceModelManager;
import org.eclipse.swt.events.HelpEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionGroup;
import org.eclipse.ui.texteditor.IDocumentProvider;

/**
 * Remote enabled version of the CEditor. If this editor is opened on a file from a remote project then it will use
 * remote versions of certain actions. If this editor is opened on a file from a local project then it defaults back to
 * normal CEditor behavior.
 */
public class RemoteCEditorInfoProvider implements IRemoteCEditorInfoProvider {
	
	private RemoteCEditor editor;
	private IEditorInput input;
	
	/**
	 * Remote Semantic highlighting manager
	 */
	private RemoteSemanticHighlightingManager fRemoteSemanticManager;
	
	private RemoteCFoldingStructureProvider fRemoteFoldingProvider; 
	
	private RemoteInactiveHighlighting fRemoteInactiveCodeHighlighting;
	

	/* (non-Javadoc)
	 * @see org.eclipse.ptp.rdt.editor.info.IRemoteCEditorInfoProvider#initializeEditor(org.eclipse.ptp.internal.rdt.editor.RemoteCEditor)
	 */
	public void initializeEditor(RemoteCEditor remoteCEditor) {
		editor = remoteCEditor;
	}

	
	protected RemoteCEditor getEditor() {
		return editor;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.ptp.rdt.editor.info.IRemoteCEditorInfoProvider#preDoSetInput(org.eclipse.ui.IEditorInput)
	 */
	public void preDoSetInput(IEditorInput input) {
		this.input = input;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ptp.rdt.editor.info.IRemoteCEditorInfoProvider#postDoSetInput(org.eclipse.ui.IEditorInput)
	 */
	public void postDoSetInput(IEditorInput input) throws CoreException {
		// nothing to do here
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ptp.rdt.editor.info.IRemoteCEditorInfoProvider#isApplicableEditorInput(org.eclipse.ui.IEditorInput)
	 */
	public int isApplicableEditorInput(IEditorInput input) {
		ICProject cproject = EditorUtility.getCProject(input);
		if (cproject == null)
			return 0;
		IProject project = cproject.getProject();
		if (RemoteNature.hasRemoteNature(project))
			return 1;
		return 0;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ptp.rdt.editor.info.IRemoteCEditorInfoProvider#getTitleImage()
	 */
	public Image getTitleImage() {
		// use default - no need to provide a new one
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ptp.rdt.editor.info.IRemoteCEditorInfoProvider#getDocumentProvider()
	 */
	public IDocumentProvider getDocumentProvider(IDocumentProvider oldProvider) {
		// uses the default document provider from CEditor - no need to provide a new one
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ptp.rdt.editor.info.IRemoteCEditorInfoProvider#getTitle(org.eclipse.ui.IEditorInput)
	 */
	public String getTitle(IEditorInput input) {
		// use default - no need to provide a new one
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ptp.rdt.editor.info.IRemoteCEditorInfoProvider#getAlternateInput(org.eclipse.ui.IEditorInput)
	 */
	public IEditorInput getAlternateInput(IEditorInput input) {
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ptp.rdt.editor.info.IRemoteCEditorInfoProvider#getTitleTooltip()
	 */
	public String getTitleTooltip() {
		// use default - no need to provide a new one
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ptp.rdt.editor.info.IRemoteCEditorInfoProvider#getOutlinePage(org.eclipse.ptp.internal.rdt.editor.RemoteCEditor)
	 */
	public CContentOutlinePage getOutlinePage(RemoteCEditor remoteCEditor) {
		if (isRemote()) {
			return new RemoteCContentOutlinePage(remoteCEditor);
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ptp.rdt.editor.info.IRemoteCEditorInfoProvider#doPostCreatePartControl(org.eclipse.swt.widgets.Composite)
	 */
	public void doPostCreatePartControl(Composite parent) {
		if (isRemote()) {
			PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, RDTHelpContextIds.REMOTE_C_CPP_EDITOR);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ptp.rdt.editor.info.IRemoteCEditorInfoProvider#getAdapter(java.lang.Class)
	 */
	public Object getAdapter(Class required) {
		if (IContextProvider.class.equals(required)) {
			return new RemoteCEditorHelpContextProvider(editor);
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ptp.rdt.editor.info.IRemoteCEditorInfoProvider#doPrePerformSave(boolean)
	 */
	public boolean doPrePerformSave(boolean overwrite) {
		// nothing to do, just return true
		return true;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ptp.rdt.editor.info.IRemoteCEditorInfoProvider#doPostPerformSave()
	 */
	public void doPostPerformSave() {
		// nothing to do

	}

	/* (non-Javadoc)
	 * @see org.eclipse.ptp.rdt.editor.info.IRemoteCEditorInfoProvider#createActions(org.eclipse.jface.text.source.IVerticalRuler)
	 */
	public void createActions(IVerticalRuler iVerticalRuler) {
		// nothing to do

	}

	/* (non-Javadoc)
	 * @see org.eclipse.ptp.rdt.editor.info.IRemoteCEditorInfoProvider#editorContextMenuAboutToShow(org.eclipse.jface.action.IMenuManager)
	 */
	public void editorContextMenuAboutToShow(IMenuManager menu) {
		if (isRemote()) {
			// remove text search
			menu.remove("org.eclipse.search.text.ctxmenu"); //$NON-NLS-1$
			// remove refactoring menu for now
			menu.remove("org.eclipse.cdt.ui.refactoring.menu"); //$NON-NLS-1$
			
			//remove some refactor menu items in the Source submenu
			IMenuManager sourceMenu = (IMenuManager) menu.find("org.eclipse.cdt.ui.source.menu"); //$NON-NLS-1$
			if (sourceMenu != null) {
				sourceMenu.remove("AddIncludeOnSelection"); //$NON-NLS-1$
				sourceMenu.remove("org.eclipse.cdt.ui.refactor.getters.and.setters"); //$NON-NLS-1$
				sourceMenu.remove("org.eclipse.cdt.ui.refactor.implement.method"); //$NON-NLS-1$
			}
			
			//remove items that don't work well for remote projects
			menu.remove("OpenMacroExplorer"); //$NON-NLS-1$
			menu.remove("ToggleSourceHeader"); //$NON-NLS-1$
			
			//quick type hierarchy
			menu.remove("OpenHierarchy"); //$NON-NLS-1$
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ptp.rdt.editor.info.IRemoteCEditorInfoProvider#dispose()
	 */
	public void dispose() {
		uninstallRemoteCodeFolding();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ptp.rdt.editor.info.IRemoteCEditorInfoProvider#getAnnotationRulerColumnWidth()
	 */
	public int getAnnotationRulerColumnWidth() {
		// default
		return 0;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ptp.rdt.editor.info.IRemoteCEditorInfoProvider#helpRequested(org.eclipse.swt.events.HelpEvent)
	 */
	public void helpRequested(HelpEvent e) {
		// nothing to do

	}

	/* (non-Javadoc)
	 * @see org.eclipse.ptp.rdt.editor.info.IRemoteCEditorInfoProvider#createUndoRedoActions()
	 */
	public void createUndoRedoActions() {
		// nothing to do

	}

	/* (non-Javadoc)
	 * @see org.eclipse.ptp.rdt.editor.info.IRemoteCEditorInfoProvider#doPreRevertToSaved()
	 */
	public void doPreRevertToSaved() {
		// nothing to do

	}

	/* (non-Javadoc)
	 * @see org.eclipse.ptp.rdt.editor.info.IRemoteCEditorInfoProvider#doPostRevertToSaved()
	 */
	public void doPostRevertToSaved() {
		// nothing to do

	}

	/* (non-Javadoc)
	 * @see org.eclipse.ptp.rdt.editor.info.IRemoteCEditorInfoProvider#aboutToBeReconciled()
	 */
	public void aboutToBeReconciled() {
		// nothing to do

	}

	/* (non-Javadoc)
	 * @see org.eclipse.ptp.rdt.editor.info.IRemoteCEditorInfoProvider#getInputCElement()
	 */
	public ICElement getInputCElement() {
		// default
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ptp.rdt.editor.info.IRemoteCEditorInfoProvider#reconciled(org.eclipse.cdt.core.dom.ast.IASTTranslationUnit, boolean, org.eclipse.core.runtime.IProgressMonitor)
	 */
	public boolean reconciled(IASTTranslationUnit ast, boolean force,
			IProgressMonitor progressMonitor) {
		// nothing to do, just return true
		return true;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ptp.rdt.editor.info.IRemoteCEditorInfoProvider#doPrePerformSaveAs(org.eclipse.core.runtime.IProgressMonitor)
	 */
	public boolean doPrePerformSaveAs(IProgressMonitor progressMonitor) {
		// nothing to do, just return true
		return true;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ptp.rdt.editor.info.IRemoteCEditorInfoProvider#doPostPerformSaveAs()
	 */
	public void doPostPerformSaveAs() {
		// nothing to do

	}

	/* (non-Javadoc)
	 * @see org.eclipse.ptp.rdt.editor.info.IRemoteCEditorInfoProvider#validateState(org.eclipse.ui.IEditorInput)
	 */
	public boolean validateState(IEditorInput input) {
		// nothing to do, just return true
		return true;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ptp.rdt.editor.info.IRemoteCEditorInfoProvider#rulerContextMenuAboutToShow(org.eclipse.jface.action.IMenuManager)
	 */
	public void rulerContextMenuAboutToShow(IMenuManager menu) {
		// nothing to do

	}

	/* (non-Javadoc)
	 * @see org.eclipse.ptp.rdt.editor.info.IRemoteCEditorInfoProvider#collectContextMenuPreferencePages()
	 */
	public String[] collectContextMenuPreferencePages() {
		// default
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ptp.rdt.editor.info.IRemoteCEditorInfoProvider#createSelectionSearchGroup(org.eclipse.ptp.internal.rdt.editor.RemoteCEditor)
	 */
	public ActionGroup createSelectionSearchGroup(RemoteCEditor remoteCEditor) {
		return new SelectionSearchGroup(remoteCEditor);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ptp.rdt.editor.info.IRemoteCEditorInfoProvider#createOpenViewActionGroup(org.eclipse.ptp.internal.rdt.editor.RemoteCEditor)
	 */
	public ActionGroup createOpenViewActionGroup(RemoteCEditor remoteCEditor) {
		return new OpenViewActionGroup(remoteCEditor);
	}
	
	/**
	 * Returns true if the input element is a C element and it comes from a remote project. Also returns true if the
	 * editor is opened on an external translation unit that was navigated to from a remote resource.
	 */
	protected boolean isRemote() {
		ICElement element = editor.getInputCElement();
		if (element == null)
			return false;
		ICProject cProject = element.getCProject();
		if (cProject == null)
			return false;
		IProject project = cProject.getProject();
		return RemoteNature.hasRemoteNature(project);
	}
	
	private boolean isLocalServiceProvider() {
		ICProject cproject = EditorUtility.getCProject(input);
		if(cproject == null) { // external translation unit
			return true;
		}
		IServiceModelManager smm = ServiceModelManager.getInstance();
		
		if(smm.isConfigured(cproject.getProject())) {
			IServiceConfiguration serviceConfig = smm.getActiveConfiguration(cproject.getProject());
			IService indexingService = smm.getService(IRDTServiceConstants.SERVICE_C_INDEX);
			IServiceProvider serviceProvider = serviceConfig.getServiceProvider(indexingService);
	
			if (serviceProvider instanceof IIndexServiceProvider) {
				return !((IIndexServiceProvider)serviceProvider).isRemote();
			}
		}
		
		if (RemoteNature.hasRemoteNature(cproject.getProject())) {
			return false;
		}

		return true;
	}

	public SourceViewerConfiguration getSourceViewerConfiguration(IPreferenceStore store, SourceViewerConfiguration sourceViewerConfiguration) {
		if (!isLocalServiceProvider()) {
			// use remote source viewer configuration
			if (!(sourceViewerConfiguration instanceof RemoteCSourceViewerConfiguration)) {
				CTextTools textTools = CUIPlugin.getDefault().getTextTools();
				return new RemoteCSourceViewerConfiguration(textTools.getColorManager(), store, editor,
						ICPartitions.C_PARTITIONING);
			}
		}
		return null;
		
	}

	public void fillActionBars(IActionBars actionBars) {
		if (isRemote()) {
			// remove refactoring for now
			actionBars.setGlobalActionHandler(CdtActionConstants.RENAME, null);
			actionBars.setGlobalActionHandler(CdtActionConstants.EXTRACT_CONSTANT, null);
			actionBars.setGlobalActionHandler(CdtActionConstants.EXTRACT_LOCAL_VARIABLE, null);
			actionBars.setGlobalActionHandler(CdtActionConstants.EXTRACT_METHOD, null);
			actionBars.setGlobalActionHandler(CdtActionConstants.HIDE_METHOD, null);
			actionBars.setGlobalActionHandler(CdtActionConstants.TOGGLE_FUNCTION, null);

			actionBars.setGlobalActionHandler(CdtActionConstants.GETTERS_AND_SETTERS, null);
			actionBars.setGlobalActionHandler(CdtActionConstants.IMPLEMENT_METHOD, null);
			actionBars.setGlobalActionHandler(CdtActionConstants.ADD_INCLUDE, null);
			actionBars.setGlobalActionHandler(CdtActionConstants.SORT_LINES, null);
		}
	}

	public boolean shouldProcessLocalParsingCompletions() {
		return isLocalServiceProvider();
	}

	/**
	 * Install Semantic Highlighting.
	 */
	public void installSemanticHighlighting(ISourceViewer sourceViewer, IPreferenceStore prefStore) {
		if (!isLocalServiceProvider() && fRemoteSemanticManager == null && isSemanticHighlightingEnabled(prefStore)) {
			fRemoteSemanticManager= new RemoteSemanticHighlightingManager();
			fRemoteSemanticManager.install(editor, (CSourceViewer) sourceViewer, CUIPlugin.getDefault().getTextTools().getColorManager(), prefStore);
		} else if (isLocalServiceProvider()) { //airplane mode
			//reset so when workspace is online, semantic highlighting gets triggered
			uninstallSemanticHighlighting();
		}
	}
	
	public void refreshRemoteSemanticManager() {
		if (fRemoteSemanticManager != null)
			fRemoteSemanticManager.refresh();
	}
	
	public boolean isSemanticHighlightingEnabled(IPreferenceStore prefStore) {
		return SemanticHighlightings.isEnabled(prefStore) && !(editor.isEnableScalablilityMode() && prefStore.getBoolean(PreferenceConstants.SCALABILITY_SEMANTIC_HIGHLIGHT));
	}
	
	public void installRemoteCodeFolding(ISourceViewer sourceViewer){
		String id= CUIPlugin.getDefault().getPreferenceStore().getString(PreferenceConstants.EDITOR_FOLDING_PROVIDER);

		// If the folding provider is the default one and we are not 
		// in off-line mode, then uninstall local folding and install remote folding:
		if (id.compareTo("org.eclipse.cdt.ui.text.defaultFoldingProvider") == 0 && !isLocalServiceProvider()) { //$NON-NLS-1$
			editor.uninstallProjectionModelUpdater();
			fRemoteFoldingProvider = new RemoteCFoldingStructureProvider();
			ProjectionViewer projectionViewer = (ProjectionViewer) sourceViewer;
			fRemoteFoldingProvider.install(editor, projectionViewer);
		}
	}
	
	public void uninstallRemoteCodeFolding() {
		if (fRemoteFoldingProvider != null)
			fRemoteFoldingProvider.uninstall();
	}

	public void uninstallSemanticHighlighting() {
		if (fRemoteSemanticManager != null) {
			fRemoteSemanticManager.uninstall();
			fRemoteSemanticManager= null;
		}
	}

	public boolean isInactiveHighlightingEnabled(IPreferenceStore prefStore) {
		return prefStore.getBoolean(CEditor.INACTIVE_CODE_ENABLE) && !editor.isEnableScalablilityMode();
	}

	public void installInactiveHighlighting(IPreferenceStore prefStore, ISharedTextColors colors) {
		if (fRemoteInactiveCodeHighlighting == null && !isLocalServiceProvider()
				&& isInactiveHighlightingEnabled(prefStore)) {
			fRemoteInactiveCodeHighlighting = new RemoteInactiveHighlighting(prefStore, colors);
			fRemoteInactiveCodeHighlighting.install(editor);
		} else if (isLocalServiceProvider()) {
			uninstallInactiveHighlighting();
		}
	}

	public void uninstallInactiveHighlighting() {
		if (fRemoteInactiveCodeHighlighting != null) {
			fRemoteInactiveCodeHighlighting.uninstall();
			fRemoteInactiveCodeHighlighting = null;
		}
	}

	public String getInactiveHighlightColorKey() {
		return RemoteInactiveHighlighting.INACTIVE_CODE_COLOR;
	}

	public void updateInactiveHighlightColor() {
		if (fRemoteInactiveCodeHighlighting != null)
			fRemoteInactiveCodeHighlighting.updateInactiveCodeColor();
	}

}

Back to the top