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:
parent
73a67bef0e
commit
cc96931d3e
2 changed files with 117 additions and 130 deletions
|
@ -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;
|
||||
|
||||
/*
|
||||
* (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.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.IProgressMonitor;
|
||||
import org.eclipse.core.runtime.NullProgressMonitor;
|
||||
|
||||
import org.eclipse.jface.text.BadLocationException;
|
||||
import org.eclipse.jface.text.IDocument;
|
||||
import org.eclipse.ui.actions.WorkspaceModifyOperation;
|
||||
import org.eclipse.ui.texteditor.ITextEditor;
|
||||
import org.eclipse.core.runtime.jobs.ISchedulingRule;
|
||||
|
||||
|
||||
/**
|
||||
|
@ -37,51 +38,45 @@ import org.eclipse.ui.texteditor.ITextEditor;
|
|||
* includes is not done. Dublicates are eliminated.
|
||||
* 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 IRequiredInclude[] fIncludes;
|
||||
private String[] fUsings;
|
||||
private boolean fDoSave;
|
||||
private ITextEditor fEditor;
|
||||
|
||||
private String newLine = System.getProperty("line.separator", "\n");
|
||||
|
||||
/**
|
||||
* Generate import statements for the passed java elements
|
||||
* Elements must be of type IType (-> single import) or IPackageFragment
|
||||
* (on-demand-import). Other JavaElements are ignored
|
||||
* Generate include statements for the passed java elements
|
||||
*/
|
||||
public AddIncludeOperation(ITextEditor ed, ITranslationUnit tu, IRequiredInclude[] includes, boolean save) {
|
||||
this (ed, tu, includes, null, save);
|
||||
public AddIncludesOperation(ITranslationUnit tu, IRequiredInclude[] includes, boolean save) {
|
||||
this (tu, includes, null, save);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate import statements for the passed java elements
|
||||
* Elements must be of type IType (-> single import) or IPackageFragment
|
||||
* (on-demand-import). Other JavaElements are ignored
|
||||
* Generate include statements for the passed c elements
|
||||
*/
|
||||
public AddIncludeOperation(ITextEditor ed, ITranslationUnit tu, IRequiredInclude[] includes, String[] using, boolean save) {
|
||||
public AddIncludesOperation(ITranslationUnit tu, IRequiredInclude[] includes, String[] using, boolean save) {
|
||||
super();
|
||||
fEditor = ed;
|
||||
fIncludes= includes;
|
||||
fUsings = using;
|
||||
fTranslationUnit = tu;
|
||||
fDoSave= save;
|
||||
}
|
||||
|
||||
public void executeInludes(ITranslationUnit root, IProgressMonitor monitor) throws CoreException {
|
||||
public void executeInludes(IProgressMonitor monitor) throws CoreException {
|
||||
// Sanity
|
||||
if (fIncludes == null || fIncludes.length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
ArrayList toAdd = new ArrayList();
|
||||
|
||||
monitor.beginTask(CEditorMessages.getString("AddIncludesOperation.description"), 2); //$NON-NLS-1$
|
||||
|
||||
if (root != null) {
|
||||
List elements = ((IParent)root).getChildrenOfType(ICElement.C_INCLUDE);
|
||||
if (fTranslationUnit != null) {
|
||||
ArrayList toAdd = new ArrayList();
|
||||
|
||||
monitor.beginTask(CEditorMessages.getString("AddIncludesOperation.description"), 2); //$NON-NLS-1$
|
||||
|
||||
List elements = fTranslationUnit.getChildrenOfType(ICElement.C_INCLUDE);
|
||||
for (int i = 0; i < fIncludes.length; ++i) {
|
||||
String name = fIncludes[i].getIncludeName();
|
||||
boolean found = false;
|
||||
|
@ -117,40 +112,25 @@ public class AddIncludeOperation extends WorkspaceModifyOperation {
|
|||
} else {
|
||||
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);
|
||||
replace(pos, insert.toString());
|
||||
monitor.worked(1);
|
||||
}
|
||||
}
|
||||
|
||||
monitor.worked(1);
|
||||
monitor.worked(1);
|
||||
}
|
||||
|
||||
public void executeUsings(ITranslationUnit root, IProgressMonitor monitor) throws CoreException {
|
||||
public void executeUsings(IProgressMonitor monitor) throws CoreException {
|
||||
// Sanity
|
||||
if (fUsings == null || fUsings.length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
ArrayList toAdd = new ArrayList();
|
||||
|
||||
monitor.beginTask(CEditorMessages.getString("AddIncludesOperation.description"), 2); //$NON-NLS-1$
|
||||
|
||||
if (root != null) {
|
||||
List elements = ((IParent)root).getChildrenOfType(ICElement.C_USING);
|
||||
if (fTranslationUnit != null) {
|
||||
ArrayList toAdd = new ArrayList();
|
||||
|
||||
monitor.beginTask(CEditorMessages.getString("AddIncludesOperation.description"), 2); //$NON-NLS-1$
|
||||
|
||||
List elements = fTranslationUnit.getChildrenOfType(ICElement.C_USING);
|
||||
for (int i = 0; i < fUsings.length; ++i) {
|
||||
String name = fUsings[i];
|
||||
boolean found = false;
|
||||
|
@ -175,12 +155,12 @@ public class AddIncludeOperation extends WorkspaceModifyOperation {
|
|||
}
|
||||
|
||||
int pos;
|
||||
List includes = ((IParent)root).getChildrenOfType(ICElement.C_INCLUDE);
|
||||
List includes = fTranslationUnit.getChildrenOfType(ICElement.C_INCLUDE);
|
||||
if (includes.size() > 0) {
|
||||
IInclude lastInclude = (IInclude)includes.get(includes.size() - 1);
|
||||
ISourceRange range = lastInclude.getSourceRange();
|
||||
pos = range.getStartPos() + range.getLength();
|
||||
} else if (elements.size() > 0){
|
||||
} else if (elements.size() > 0) {
|
||||
IUsing lastUsing = (IUsing)includes.get(includes.size() - 1);
|
||||
ISourceRange range = lastUsing.getSourceRange();
|
||||
pos = range.getStartPos() + range.getLength();
|
||||
|
@ -188,51 +168,50 @@ public class AddIncludeOperation extends WorkspaceModifyOperation {
|
|||
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);
|
||||
replace(pos, insert.toString());
|
||||
monitor.worked(1);
|
||||
}
|
||||
}
|
||||
monitor.worked(1);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.eclipse.ui.actions.WorkspaceModifyOperation#execute(org.eclipse.core.runtime.IProgressMonitor)
|
||||
*/
|
||||
protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException {
|
||||
void replace(int pos, String s) {
|
||||
try {
|
||||
IBuffer buffer = fTranslationUnit.getBuffer();
|
||||
// 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) {
|
||||
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 {
|
||||
executeUsings(root, monitor);
|
||||
executeInludes(root, monitor);
|
||||
executeUsings(monitor);
|
||||
executeInludes(monitor);
|
||||
} finally {
|
||||
monitor.done();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Returns the scheduling rule for this operation
|
||||
*/
|
||||
public ISchedulingRule getScheduleRule() {
|
||||
return ResourcesPlugin.getWorkspace().getRoot();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -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;
|
||||
|
||||
/*
|
||||
* (c) Copyright IBM Corp. 2000, 2001.
|
||||
* All Rights Reserved.
|
||||
*/
|
||||
|
||||
import org.eclipse.cdt.core.browser.AllTypesCache;
|
||||
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.internal.ui.CCompletionContributorManager;
|
||||
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.ui.CSearchResultLabelProvider;
|
||||
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.dialogs.MessageDialog;
|
||||
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
|
||||
import org.eclipse.jface.operation.IRunnableWithProgress;
|
||||
import org.eclipse.jface.text.BadLocationException;
|
||||
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) {
|
||||
super(CEditorMessages.getString("AddIncludeOnSelection.label")); //$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) {
|
||||
AddIncludeOperation op= new AddIncludeOperation(fEditor, tu, fRequiredIncludes, fUsings, false);
|
||||
AddIncludesOperation op= new AddIncludesOperation(tu, fRequiredIncludes, fUsings, false);
|
||||
try {
|
||||
ProgressMonitorDialog dialog= new ProgressMonitorDialog(getShell());
|
||||
dialog.run(false, true, op);
|
||||
PlatformUI.getWorkbench().getProgressService().runInUI(
|
||||
PlatformUI.getWorkbench().getProgressService(),
|
||||
new WorkbenchRunnableAdapter(op), op.getScheduleRule());
|
||||
} catch (InvocationTargetException e) {
|
||||
//e.printStackTrace();
|
||||
MessageDialog.openError(getShell(), CEditorMessages.getString("AddIncludeOnSelection.error.message1"), e.getTargetException().getMessage()); //$NON-NLS-1$
|
||||
ExceptionHandler.handle(e, getShell(), CEditorMessages.getString("AddIncludeOnSelection.error.message1"), null); //$NON-NLS-1$
|
||||
} catch (InterruptedException e) {
|
||||
// Do nothing. Operation has been canceled.
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ITranslationUnit getTranslationUnit () {
|
||||
private ITranslationUnit getTranslationUnit () {
|
||||
ITranslationUnit unit = null;
|
||||
if (fEditor != null) {
|
||||
IEditorInput editorInput= fEditor.getEditorInput();
|
||||
|
@ -123,7 +125,7 @@ public class AddIncludeOnSelectionAction extends Action implements IUpdate {
|
|||
return unit;
|
||||
}
|
||||
|
||||
protected Shell getShell() {
|
||||
private Shell getShell() {
|
||||
return fEditor.getSite().getShell();
|
||||
}
|
||||
|
||||
|
@ -185,8 +187,12 @@ public class AddIncludeOnSelectionAction extends Action implements IUpdate {
|
|||
return;
|
||||
}
|
||||
|
||||
// Try contributed plugins.
|
||||
findContribution(name);
|
||||
// Try contribution from plugins.
|
||||
IFunctionSummary fs = findContribution(name);
|
||||
if (fs != null) {
|
||||
fRequiredIncludes = fs.getIncludes();
|
||||
fUsings = new String[] {fs.getNamespace()};
|
||||
}
|
||||
|
||||
// Try the type caching.
|
||||
if (fRequiredIncludes == null && fUsings == null) {
|
||||
|
@ -209,18 +215,20 @@ public class AddIncludeOnSelectionAction extends Action implements IUpdate {
|
|||
|
||||
}
|
||||
|
||||
void findContribution (String name) {
|
||||
IFunctionSummary fs = CCompletionContributorManager.getDefault().getFunctionInfo(name);
|
||||
if(fs != null) {
|
||||
fRequiredIncludes = fs.getIncludes();
|
||||
fUsings = new String[] {fs.getNamespace()};
|
||||
}
|
||||
private IFunctionSummary findContribution (final String name) {
|
||||
final IFunctionSummary[] fs = new IFunctionSummary[1];
|
||||
IRunnableWithProgress op = new IRunnableWithProgress() {
|
||||
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
|
||||
fs[0] = CCompletionContributorManager.getDefault().getFunctionInfo(name);
|
||||
}
|
||||
};
|
||||
return fs[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a type by the simple name.
|
||||
*/
|
||||
ITypeInfo[] findTypeInfos(final String name) {
|
||||
private ITypeInfo[] findTypeInfos(final String name) {
|
||||
final ITypeInfo[][] infos = new ITypeInfo[1][];
|
||||
IRunnableWithProgress op = new IRunnableWithProgress() {
|
||||
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
|
||||
|
@ -241,7 +249,7 @@ public class AddIncludeOnSelectionAction extends Action implements IUpdate {
|
|||
return infos[0];
|
||||
}
|
||||
|
||||
IMatch[] findMatches(final String name) {
|
||||
private IMatch[] findMatches(final String name) {
|
||||
final BasicSearchResultCollector searchResultCollector = new BasicSearchResultCollector();
|
||||
IRunnableWithProgress op = new IRunnableWithProgress() {
|
||||
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
|
||||
|
|
Loading…
Add table
Reference in a new issue