1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

More support for "add include"

This commit is contained in:
Alain Magloire 2004-07-29 02:37:45 +00:00
parent 73a67bef0e
commit cc96931d3e
2 changed files with 117 additions and 130 deletions

View file

@ -1,34 +1,35 @@
/*******************************************************************************
* Copyright (c) 2000, 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
* QNX Software Systems
*******************************************************************************/
package org.eclipse.cdt.internal.ui.codemanipulation; package org.eclipse.cdt.internal.ui.codemanipulation;
/*
* (c) Copyright IBM Corp. 2000, 2001.
* All Rights Reserved.
*/
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.IInclude;
import org.eclipse.cdt.core.model.IParent;
import org.eclipse.cdt.core.model.ISourceRange;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.model.IUsing;
import org.eclipse.cdt.internal.ui.editor.CEditor;
import org.eclipse.cdt.internal.ui.editor.CEditorMessages;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.IRequiredInclude;
import org.eclipse.cdt.ui.IWorkingCopyManager;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.eclipse.cdt.core.model.IBuffer;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.IInclude;
import org.eclipse.cdt.core.model.ISourceRange;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.model.IUsing;
import org.eclipse.cdt.internal.ui.editor.CEditorMessages;
import org.eclipse.cdt.ui.IRequiredInclude;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ui.actions.WorkspaceModifyOperation;
import org.eclipse.ui.texteditor.ITextEditor;
/** /**
@ -37,51 +38,45 @@ import org.eclipse.ui.texteditor.ITextEditor;
* includes is not done. Dublicates are eliminated. * includes is not done. Dublicates are eliminated.
* If the translation unit is open in an editor, be sure to pass over its working copy. * If the translation unit is open in an editor, be sure to pass over its working copy.
*/ */
public class AddIncludeOperation extends WorkspaceModifyOperation { public class AddIncludesOperation implements IWorkspaceRunnable {
private ITranslationUnit fTranslationUnit; private ITranslationUnit fTranslationUnit;
private IRequiredInclude[] fIncludes; private IRequiredInclude[] fIncludes;
private String[] fUsings; private String[] fUsings;
private boolean fDoSave; private boolean fDoSave;
private ITextEditor fEditor;
private String newLine = System.getProperty("line.separator", "\n"); private String newLine = System.getProperty("line.separator", "\n");
/** /**
* Generate import statements for the passed java elements * Generate include statements for the passed java elements
* Elements must be of type IType (-> single import) or IPackageFragment
* (on-demand-import). Other JavaElements are ignored
*/ */
public AddIncludeOperation(ITextEditor ed, ITranslationUnit tu, IRequiredInclude[] includes, boolean save) { public AddIncludesOperation(ITranslationUnit tu, IRequiredInclude[] includes, boolean save) {
this (ed, tu, includes, null, save); this (tu, includes, null, save);
} }
/** /**
* Generate import statements for the passed java elements * Generate include statements for the passed c elements
* Elements must be of type IType (-> single import) or IPackageFragment
* (on-demand-import). Other JavaElements are ignored
*/ */
public AddIncludeOperation(ITextEditor ed, ITranslationUnit tu, IRequiredInclude[] includes, String[] using, boolean save) { public AddIncludesOperation(ITranslationUnit tu, IRequiredInclude[] includes, String[] using, boolean save) {
super(); super();
fEditor = ed;
fIncludes= includes; fIncludes= includes;
fUsings = using; fUsings = using;
fTranslationUnit = tu; fTranslationUnit = tu;
fDoSave= save; fDoSave= save;
} }
public void executeInludes(ITranslationUnit root, IProgressMonitor monitor) throws CoreException { public void executeInludes(IProgressMonitor monitor) throws CoreException {
// Sanity // Sanity
if (fIncludes == null || fIncludes.length == 0) { if (fIncludes == null || fIncludes.length == 0) {
return; return;
} }
if (fTranslationUnit != null) {
ArrayList toAdd = new ArrayList(); ArrayList toAdd = new ArrayList();
monitor.beginTask(CEditorMessages.getString("AddIncludesOperation.description"), 2); //$NON-NLS-1$ monitor.beginTask(CEditorMessages.getString("AddIncludesOperation.description"), 2); //$NON-NLS-1$
if (root != null) { List elements = fTranslationUnit.getChildrenOfType(ICElement.C_INCLUDE);
List elements = ((IParent)root).getChildrenOfType(ICElement.C_INCLUDE);
for (int i = 0; i < fIncludes.length; ++i) { for (int i = 0; i < fIncludes.length; ++i) {
String name = fIncludes[i].getIncludeName(); String name = fIncludes[i].getIncludeName();
boolean found = false; boolean found = false;
@ -117,40 +112,25 @@ public class AddIncludeOperation extends WorkspaceModifyOperation {
} else { } else {
pos = 0; pos = 0;
} }
IDocument document = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
try {
// Now find the next newline and insert after that
if (pos > 0) {
while (document.getChar(pos) != '\n') {
pos++;
}
if (document.getChar(pos) == '\r') {
pos++;
}
pos++;
}
document.replace(pos, 0, insert.toString());
} catch (BadLocationException e) {}
}
}
monitor.worked(1); monitor.worked(1);
replace(pos, insert.toString());
monitor.worked(1); monitor.worked(1);
} }
}
}
public void executeUsings(ITranslationUnit root, IProgressMonitor monitor) throws CoreException { public void executeUsings(IProgressMonitor monitor) throws CoreException {
// Sanity // Sanity
if (fUsings == null || fUsings.length == 0) { if (fUsings == null || fUsings.length == 0) {
return; return;
} }
if (fTranslationUnit != null) {
ArrayList toAdd = new ArrayList(); ArrayList toAdd = new ArrayList();
monitor.beginTask(CEditorMessages.getString("AddIncludesOperation.description"), 2); //$NON-NLS-1$ monitor.beginTask(CEditorMessages.getString("AddIncludesOperation.description"), 2); //$NON-NLS-1$
if (root != null) { List elements = fTranslationUnit.getChildrenOfType(ICElement.C_USING);
List elements = ((IParent)root).getChildrenOfType(ICElement.C_USING);
for (int i = 0; i < fUsings.length; ++i) { for (int i = 0; i < fUsings.length; ++i) {
String name = fUsings[i]; String name = fUsings[i];
boolean found = false; boolean found = false;
@ -175,12 +155,12 @@ public class AddIncludeOperation extends WorkspaceModifyOperation {
} }
int pos; int pos;
List includes = ((IParent)root).getChildrenOfType(ICElement.C_INCLUDE); List includes = fTranslationUnit.getChildrenOfType(ICElement.C_INCLUDE);
if (includes.size() > 0) { if (includes.size() > 0) {
IInclude lastInclude = (IInclude)includes.get(includes.size() - 1); IInclude lastInclude = (IInclude)includes.get(includes.size() - 1);
ISourceRange range = lastInclude.getSourceRange(); ISourceRange range = lastInclude.getSourceRange();
pos = range.getStartPos() + range.getLength(); pos = range.getStartPos() + range.getLength();
} else if (elements.size() > 0){ } else if (elements.size() > 0) {
IUsing lastUsing = (IUsing)includes.get(includes.size() - 1); IUsing lastUsing = (IUsing)includes.get(includes.size() - 1);
ISourceRange range = lastUsing.getSourceRange(); ISourceRange range = lastUsing.getSourceRange();
pos = range.getStartPos() + range.getLength(); pos = range.getStartPos() + range.getLength();
@ -188,51 +168,50 @@ public class AddIncludeOperation extends WorkspaceModifyOperation {
pos = 0; pos = 0;
} }
IDocument document = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput()); monitor.worked(1);
try { replace(pos, insert.toString());
// Now find the next newline and insert after that
if (pos > 0) {
while (document.getChar(pos) != '\n') {
pos++;
}
if (document.getChar(pos) == '\r') {
pos++;
}
pos++;
}
document.replace(pos, 0, insert.toString());
} catch (BadLocationException e) {}
}
}
monitor.worked(1); monitor.worked(1);
} }
}
}
/* (non-Javadoc) void replace(int pos, String s) {
* @see org.eclipse.ui.actions.WorkspaceModifyOperation#execute(org.eclipse.core.runtime.IProgressMonitor) try {
*/ IBuffer buffer = fTranslationUnit.getBuffer();
protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException { // Now find the next newline and insert after that
if (pos > 0) {
while (buffer.getChar(pos) != '\n') {
pos++;
}
if (buffer.getChar(pos) == '\r') {
pos++;
}
pos++;
}
buffer.replace(pos, 0, s);
} catch (Exception e) {
// ignore; should we log ?
}
}
public void run(IProgressMonitor monitor) throws CoreException {
if (monitor == null) { if (monitor == null) {
monitor= new NullProgressMonitor(); monitor= new NullProgressMonitor();
} }
ITranslationUnit root;
// Look in content outline
if (fEditor instanceof CEditor) {
IWorkingCopyManager mgr = CUIPlugin.getDefault().getWorkingCopyManager();
root = mgr.getWorkingCopy(fEditor.getEditorInput());
} else {
root = fTranslationUnit;
}
try { try {
executeUsings(root, monitor); executeUsings(monitor);
executeInludes(root, monitor); executeInludes(monitor);
} finally { } finally {
monitor.done(); monitor.done();
} }
} }
/**
* @return Returns the scheduling rule for this operation
*/
public ISchedulingRule getScheduleRule() {
return ResourcesPlugin.getWorkspace().getRoot();
}
} }

View file

@ -1,9 +1,16 @@
/*******************************************************************************
* Copyright (c) 2000, 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
* QNX Software Systems
*******************************************************************************/
package org.eclipse.cdt.internal.ui.editor; package org.eclipse.cdt.internal.ui.editor;
/*
* (c) Copyright IBM Corp. 2000, 2001.
* All Rights Reserved.
*/
import org.eclipse.cdt.core.browser.AllTypesCache; import org.eclipse.cdt.core.browser.AllTypesCache;
import org.eclipse.cdt.core.browser.ITypeInfo; import org.eclipse.cdt.core.browser.ITypeInfo;
@ -22,7 +29,8 @@ import org.eclipse.cdt.core.search.OrPattern;
import org.eclipse.cdt.core.search.SearchEngine; import org.eclipse.cdt.core.search.SearchEngine;
import org.eclipse.cdt.internal.ui.CCompletionContributorManager; import org.eclipse.cdt.internal.ui.CCompletionContributorManager;
import org.eclipse.cdt.internal.ui.ICHelpContextIds; import org.eclipse.cdt.internal.ui.ICHelpContextIds;
import org.eclipse.cdt.internal.ui.codemanipulation.AddIncludeOperation; import org.eclipse.cdt.internal.ui.actions.WorkbenchRunnableAdapter;
import org.eclipse.cdt.internal.ui.codemanipulation.AddIncludesOperation;
import org.eclipse.cdt.internal.ui.util.ExceptionHandler; import org.eclipse.cdt.internal.ui.util.ExceptionHandler;
import org.eclipse.cdt.ui.CSearchResultLabelProvider; import org.eclipse.cdt.ui.CSearchResultLabelProvider;
import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.ui.CUIPlugin;
@ -41,7 +49,6 @@ import org.eclipse.swt.widgets.Shell;
import org.eclipse.jface.action.Action; import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress; import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IDocument;
@ -86,11 +93,6 @@ public class AddIncludeOnSelectionAction extends Action implements IUpdate {
} }
public AddIncludeOnSelectionAction() {
this(null);
}
public AddIncludeOnSelectionAction(ITextEditor editor) { public AddIncludeOnSelectionAction(ITextEditor editor) {
super(CEditorMessages.getString("AddIncludeOnSelection.label")); //$NON-NLS-1$ super(CEditorMessages.getString("AddIncludeOnSelection.label")); //$NON-NLS-1$
setToolTipText(CEditorMessages.getString("AddIncludeOnSelection.tooltip")); //$NON-NLS-1$ setToolTipText(CEditorMessages.getString("AddIncludeOnSelection.tooltip")); //$NON-NLS-1$
@ -101,20 +103,20 @@ public class AddIncludeOnSelectionAction extends Action implements IUpdate {
} }
private void addInclude(ITranslationUnit tu) { private void addInclude(ITranslationUnit tu) {
AddIncludeOperation op= new AddIncludeOperation(fEditor, tu, fRequiredIncludes, fUsings, false); AddIncludesOperation op= new AddIncludesOperation(tu, fRequiredIncludes, fUsings, false);
try { try {
ProgressMonitorDialog dialog= new ProgressMonitorDialog(getShell()); PlatformUI.getWorkbench().getProgressService().runInUI(
dialog.run(false, true, op); PlatformUI.getWorkbench().getProgressService(),
new WorkbenchRunnableAdapter(op), op.getScheduleRule());
} catch (InvocationTargetException e) { } catch (InvocationTargetException e) {
//e.printStackTrace(); ExceptionHandler.handle(e, getShell(), CEditorMessages.getString("AddIncludeOnSelection.error.message1"), null); //$NON-NLS-1$
MessageDialog.openError(getShell(), CEditorMessages.getString("AddIncludeOnSelection.error.message1"), e.getTargetException().getMessage()); //$NON-NLS-1$
} catch (InterruptedException e) { } catch (InterruptedException e) {
// Do nothing. Operation has been canceled. // Do nothing. Operation has been canceled.
} }
} }
ITranslationUnit getTranslationUnit () { private ITranslationUnit getTranslationUnit () {
ITranslationUnit unit = null; ITranslationUnit unit = null;
if (fEditor != null) { if (fEditor != null) {
IEditorInput editorInput= fEditor.getEditorInput(); IEditorInput editorInput= fEditor.getEditorInput();
@ -123,7 +125,7 @@ public class AddIncludeOnSelectionAction extends Action implements IUpdate {
return unit; return unit;
} }
protected Shell getShell() { private Shell getShell() {
return fEditor.getSite().getShell(); return fEditor.getSite().getShell();
} }
@ -185,8 +187,12 @@ public class AddIncludeOnSelectionAction extends Action implements IUpdate {
return; return;
} }
// Try contributed plugins. // Try contribution from plugins.
findContribution(name); IFunctionSummary fs = findContribution(name);
if (fs != null) {
fRequiredIncludes = fs.getIncludes();
fUsings = new String[] {fs.getNamespace()};
}
// Try the type caching. // Try the type caching.
if (fRequiredIncludes == null && fUsings == null) { if (fRequiredIncludes == null && fUsings == null) {
@ -209,18 +215,20 @@ public class AddIncludeOnSelectionAction extends Action implements IUpdate {
} }
void findContribution (String name) { private IFunctionSummary findContribution (final String name) {
IFunctionSummary fs = CCompletionContributorManager.getDefault().getFunctionInfo(name); final IFunctionSummary[] fs = new IFunctionSummary[1];
if(fs != null) { IRunnableWithProgress op = new IRunnableWithProgress() {
fRequiredIncludes = fs.getIncludes(); public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
fUsings = new String[] {fs.getNamespace()}; fs[0] = CCompletionContributorManager.getDefault().getFunctionInfo(name);
} }
};
return fs[0];
} }
/** /**
* Finds a type by the simple name. * Finds a type by the simple name.
*/ */
ITypeInfo[] findTypeInfos(final String name) { private ITypeInfo[] findTypeInfos(final String name) {
final ITypeInfo[][] infos = new ITypeInfo[1][]; final ITypeInfo[][] infos = new ITypeInfo[1][];
IRunnableWithProgress op = new IRunnableWithProgress() { IRunnableWithProgress op = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
@ -241,7 +249,7 @@ public class AddIncludeOnSelectionAction extends Action implements IUpdate {
return infos[0]; return infos[0];
} }
IMatch[] findMatches(final String name) { private IMatch[] findMatches(final String name) {
final BasicSearchResultCollector searchResultCollector = new BasicSearchResultCollector(); final BasicSearchResultCollector searchResultCollector = new BasicSearchResultCollector();
IRunnableWithProgress op = new IRunnableWithProgress() { IRunnableWithProgress op = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {