mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Add Include fixes. Bugs 284669 and 295174.
This commit is contained in:
parent
760126e883
commit
3f8ba3ef71
2 changed files with 210 additions and 199 deletions
|
@ -25,205 +25,46 @@ import org.eclipse.core.runtime.NullProgressMonitor;
|
||||||
import org.eclipse.core.runtime.jobs.ISchedulingRule;
|
import org.eclipse.core.runtime.jobs.ISchedulingRule;
|
||||||
import org.eclipse.jface.text.BadLocationException;
|
import org.eclipse.jface.text.BadLocationException;
|
||||||
import org.eclipse.jface.text.IDocument;
|
import org.eclipse.jface.text.IDocument;
|
||||||
import org.eclipse.jface.text.IRegion;
|
import org.eclipse.text.edits.InsertEdit;
|
||||||
import org.eclipse.jface.text.Region;
|
|
||||||
|
|
||||||
import org.eclipse.cdt.core.model.CModelException;
|
import org.eclipse.cdt.core.model.CModelException;
|
||||||
import org.eclipse.cdt.core.model.IBuffer;
|
import org.eclipse.cdt.core.model.IBuffer;
|
||||||
import org.eclipse.cdt.core.model.ICElement;
|
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.ISourceRange;
|
||||||
|
import org.eclipse.cdt.core.model.ISourceReference;
|
||||||
import org.eclipse.cdt.core.model.ITranslationUnit;
|
import org.eclipse.cdt.core.model.ITranslationUnit;
|
||||||
import org.eclipse.cdt.core.model.IUsing;
|
|
||||||
import org.eclipse.cdt.ui.IRequiredInclude;
|
import org.eclipse.cdt.ui.IRequiredInclude;
|
||||||
|
|
||||||
import org.eclipse.cdt.internal.ui.editor.CEditorMessages;
|
import org.eclipse.cdt.internal.ui.editor.CEditorMessages;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add includes to a translation unit.
|
* Adds includes and 'using' declarations to a translation unit.
|
||||||
* The input is an array of full qualified type names. No elimination of unnecessary
|
|
||||||
* includes is not done. Duplicates 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 AddIncludesOperation implements IWorkspaceRunnable {
|
public class AddIncludesOperation implements IWorkspaceRunnable {
|
||||||
private ITranslationUnit fTranslationUnit;
|
private final ITranslationUnit fTranslationUnit;
|
||||||
private IRequiredInclude[] fIncludes;
|
private final int fBeforeOffset;
|
||||||
private String[] fUsings;
|
private final IRequiredInclude[] fIncludes;
|
||||||
private final String fNewLine;
|
private final String[] fUsings;
|
||||||
private IRegion insertedIncludes;
|
private String fNewLine;
|
||||||
|
private IBuffer fBuffer;
|
||||||
|
private List<ICElement> fExistingIncludes;
|
||||||
|
private List<ICElement> fExistingUsings;
|
||||||
|
private InsertEdit fIncludesInsert;
|
||||||
|
private InsertEdit fUsingsInsert;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate include statements for the passed java elements
|
* @param tu a translation unit.
|
||||||
|
* @param beforeOffset includes and 'using' declarations have to be inserted before this offset.
|
||||||
|
* @param includes '#include' statements to insert.
|
||||||
|
* @param usings 'using' statements to insert.
|
||||||
*/
|
*/
|
||||||
public AddIncludesOperation(ITranslationUnit tu, IRequiredInclude[] includes, boolean save) {
|
public AddIncludesOperation(ITranslationUnit tu, int beforeOffset, IRequiredInclude[] includes,
|
||||||
this(tu, includes, null, save);
|
String[] usings) {
|
||||||
}
|
fTranslationUnit = tu;
|
||||||
|
fBeforeOffset = beforeOffset;
|
||||||
/**
|
|
||||||
* Generate include statements for the passed c elements
|
|
||||||
*/
|
|
||||||
public AddIncludesOperation(ITranslationUnit tu, IRequiredInclude[] includes, String[] usings,
|
|
||||||
boolean save) {
|
|
||||||
super();
|
|
||||||
fIncludes= includes;
|
fIncludes= includes;
|
||||||
fUsings = usings;
|
fUsings = usings;
|
||||||
fTranslationUnit = tu;
|
|
||||||
fNewLine= getNewLine(tu);
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getNewLine(ITranslationUnit tu) {
|
|
||||||
try {
|
|
||||||
IBuffer buf= tu.getBuffer();
|
|
||||||
if (buf instanceof IAdaptable) {
|
|
||||||
IDocument doc= (IDocument) ((IAdaptable) buf).getAdapter(IDocument.class);
|
|
||||||
if (doc != null) {
|
|
||||||
String delim= doc.getLineDelimiter(0);
|
|
||||||
if (delim != null) {
|
|
||||||
return delim;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (CModelException e) {
|
|
||||||
} catch (BadLocationException e) {
|
|
||||||
}
|
|
||||||
return System.getProperty("line.separator", "\n"); //$NON-NLS-1$//$NON-NLS-2$
|
|
||||||
}
|
|
||||||
|
|
||||||
private void insertIncludes(IProgressMonitor monitor) throws CoreException {
|
|
||||||
// Sanity
|
|
||||||
if (fIncludes == null || fIncludes.length == 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fTranslationUnit != null) {
|
|
||||||
ArrayList<IRequiredInclude> toAdd = new ArrayList<IRequiredInclude>();
|
|
||||||
|
|
||||||
monitor.beginTask(CEditorMessages.AddIncludesOperation_description, 2);
|
|
||||||
|
|
||||||
List<ICElement> elements = fTranslationUnit.getChildrenOfType(ICElement.C_INCLUDE);
|
|
||||||
for (IRequiredInclude include : fIncludes) {
|
|
||||||
String name = include.getIncludeName();
|
|
||||||
boolean found = false;
|
|
||||||
for (ICElement element : elements) {
|
|
||||||
if (name.equals(element.getElementName())) {
|
|
||||||
found = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!found) {
|
|
||||||
toAdd.add(include);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!toAdd.isEmpty()) {
|
|
||||||
// So we have our list. Now insert.
|
|
||||||
StringBuilder buf = new StringBuilder();
|
|
||||||
for (IRequiredInclude include : toAdd) {
|
|
||||||
if (include.isStandard()) {
|
|
||||||
buf.append("#include <" + include.getIncludeName() + ">").append(fNewLine); //$NON-NLS-1$ //$NON-NLS-2$
|
|
||||||
} else {
|
|
||||||
buf.append("#include \"" + include.getIncludeName() + "\"").append(fNewLine); //$NON-NLS-1$ //$NON-NLS-2$
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int pos = 0;
|
|
||||||
if (!elements.isEmpty()) {
|
|
||||||
IInclude lastInclude = (IInclude) elements.get(elements.size() - 1);
|
|
||||||
ISourceRange range = lastInclude.getSourceRange();
|
|
||||||
pos = range.getStartPos() + range.getLength();
|
|
||||||
}
|
|
||||||
monitor.worked(1);
|
|
||||||
replace(pos, buf.toString());
|
|
||||||
insertedIncludes = new Region(pos, buf.length());
|
|
||||||
monitor.worked(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void insertUsings(IProgressMonitor monitor) throws CoreException {
|
|
||||||
// Sanity
|
|
||||||
if (fUsings == null || fUsings.length == 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fTranslationUnit != null) {
|
|
||||||
ArrayList<String> toAdd = new ArrayList<String>(fUsings.length);
|
|
||||||
|
|
||||||
monitor.beginTask(CEditorMessages.AddIncludesOperation_description, 2);
|
|
||||||
|
|
||||||
List<ICElement> elements = fTranslationUnit.getChildrenOfType(ICElement.C_USING);
|
|
||||||
for (String name : fUsings) {
|
|
||||||
boolean found = false;
|
|
||||||
for (ICElement element : elements) {
|
|
||||||
if (name.equals(element.getElementName())) {
|
|
||||||
found = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!found) {
|
|
||||||
toAdd.add(name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!toAdd.isEmpty()) {
|
|
||||||
// So we have our list. Now insert.
|
|
||||||
StringBuilder buf = new StringBuilder();
|
|
||||||
for (String using : toAdd) {
|
|
||||||
buf.append("using ").append(using).append(';').append(fNewLine); //$NON-NLS-1$
|
|
||||||
}
|
|
||||||
|
|
||||||
int pos = 0;
|
|
||||||
if (!elements.isEmpty()) {
|
|
||||||
IUsing lastUsing = (IUsing) elements.get(elements.size() - 1);
|
|
||||||
ISourceRange range = lastUsing.getSourceRange();
|
|
||||||
pos = range.getStartPos() + range.getLength();
|
|
||||||
} else {
|
|
||||||
List<ICElement> includes = fTranslationUnit.getChildrenOfType(ICElement.C_INCLUDE);
|
|
||||||
if (!includes.isEmpty()) {
|
|
||||||
IInclude lastInclude = (IInclude) includes.get(includes.size() - 1);
|
|
||||||
ISourceRange range = lastInclude.getSourceRange();
|
|
||||||
pos = range.getStartPos() + range.getLength();
|
|
||||||
}
|
|
||||||
if (!includes.isEmpty() || insertedIncludes != null) {
|
|
||||||
buf.insert(0, fNewLine);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (insertedIncludes != null && pos >= insertedIncludes.getOffset()) {
|
|
||||||
pos += insertedIncludes.getLength();
|
|
||||||
}
|
|
||||||
|
|
||||||
monitor.worked(1);
|
|
||||||
replace(pos, buf.toString());
|
|
||||||
monitor.worked(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void replace(int pos, String s) throws CModelException {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void run(IProgressMonitor monitor) throws CoreException {
|
|
||||||
if (monitor == null) {
|
|
||||||
monitor= new NullProgressMonitor();
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
insertIncludes(monitor);
|
|
||||||
insertUsings(monitor);
|
|
||||||
} finally {
|
|
||||||
monitor.done();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -232,4 +73,165 @@ public class AddIncludesOperation implements IWorkspaceRunnable {
|
||||||
public ISchedulingRule getSchedulingRule() {
|
public ISchedulingRule getSchedulingRule() {
|
||||||
return ResourcesPlugin.getWorkspace().getRoot();
|
return ResourcesPlugin.getWorkspace().getRoot();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void run(IProgressMonitor monitor) throws CoreException {
|
||||||
|
if (monitor == null) {
|
||||||
|
monitor= new NullProgressMonitor();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
monitor.beginTask(CEditorMessages.AddIncludesOperation_description, 3);
|
||||||
|
|
||||||
|
fBuffer = fTranslationUnit.getBuffer();
|
||||||
|
fNewLine= getLineSeparator();
|
||||||
|
fExistingIncludes = fTranslationUnit.getChildrenOfType(ICElement.C_INCLUDE);
|
||||||
|
fIncludesInsert = getIncludesInsert();
|
||||||
|
monitor.worked(1);
|
||||||
|
if (fUsings != null && fUsings.length > 0) {
|
||||||
|
fExistingUsings = fTranslationUnit.getChildrenOfType(ICElement.C_USING);
|
||||||
|
}
|
||||||
|
fUsingsInsert = getUsingsInsert();
|
||||||
|
monitor.worked(1);
|
||||||
|
|
||||||
|
if (fIncludesInsert != null) {
|
||||||
|
fBuffer.replace(fIncludesInsert.getOffset(), 0, fIncludesInsert.getText());
|
||||||
|
}
|
||||||
|
if (fUsingsInsert != null) {
|
||||||
|
int offset = fUsingsInsert.getOffset();
|
||||||
|
if (fIncludesInsert != null && offset >= fIncludesInsert.getOffset()) {
|
||||||
|
offset += fIncludesInsert.getText().length();
|
||||||
|
}
|
||||||
|
fBuffer.replace(offset, 0, fUsingsInsert.getText());
|
||||||
|
}
|
||||||
|
monitor.worked(1);
|
||||||
|
} finally {
|
||||||
|
monitor.done();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private InsertEdit getIncludesInsert() throws CoreException {
|
||||||
|
if (fIncludes == null || fIncludes.length == 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
ArrayList<IRequiredInclude> toAdd = new ArrayList<IRequiredInclude>();
|
||||||
|
for (IRequiredInclude include : fIncludes) {
|
||||||
|
String name = include.getIncludeName();
|
||||||
|
boolean found = false;
|
||||||
|
for (ICElement element : fExistingIncludes) {
|
||||||
|
ISourceRange range = ((ISourceReference) element).getSourceRange();
|
||||||
|
if (range.getStartPos() + range.getLength() > fBeforeOffset) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (name.equals(element.getElementName())) {
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!found) {
|
||||||
|
toAdd.add(include);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (toAdd.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// So we have our list. Now insert.
|
||||||
|
StringBuilder buf = new StringBuilder();
|
||||||
|
for (IRequiredInclude include : toAdd) {
|
||||||
|
if (include.isStandard()) {
|
||||||
|
buf.append("#include <" + include.getIncludeName() + ">").append(fNewLine); //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
|
} else {
|
||||||
|
buf.append("#include \"" + include.getIncludeName() + "\"").append(fNewLine); //$NON-NLS-1$ //$NON-NLS-2$
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int pos = getOffsetAfterLast(fExistingIncludes);
|
||||||
|
return new InsertEdit(pos, buf.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
private InsertEdit getUsingsInsert() throws CoreException {
|
||||||
|
if (fUsings == null || fUsings.length == 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
ArrayList<String> toAdd = new ArrayList<String>(fUsings.length);
|
||||||
|
for (String name : fUsings) {
|
||||||
|
boolean found = false;
|
||||||
|
for (ICElement element : fExistingUsings) {
|
||||||
|
ISourceRange range = ((ISourceReference) element).getSourceRange();
|
||||||
|
if (range.getStartPos() + range.getLength() > fBeforeOffset) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (name.equals(element.getElementName())) {
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!found) {
|
||||||
|
toAdd.add(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (toAdd.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// So we have our list. Now insert.
|
||||||
|
StringBuilder buf = new StringBuilder();
|
||||||
|
for (String using : toAdd) {
|
||||||
|
buf.append("using ").append(using).append(';').append(fNewLine); //$NON-NLS-1$
|
||||||
|
}
|
||||||
|
|
||||||
|
int pos = getOffsetAfterLast(fExistingUsings);
|
||||||
|
int pos2 = getOffsetAfterLast(fExistingIncludes);
|
||||||
|
if (pos <= pos2) {
|
||||||
|
pos = pos2;
|
||||||
|
buf.insert(0, fNewLine); // Add a blank line between #include and using statements.
|
||||||
|
}
|
||||||
|
|
||||||
|
return new InsertEdit(pos, buf.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find the last of elements located before fBeforeOffset and returns offset of the following line.
|
||||||
|
* @param elements source elements to consider.
|
||||||
|
* @return offset of the line after the last of elements located before fBeforeOffset, or
|
||||||
|
* zero, if there is no such element.
|
||||||
|
* @throws CModelException
|
||||||
|
*/
|
||||||
|
private int getOffsetAfterLast(List<ICElement> elements) throws CModelException {
|
||||||
|
for (int i = elements.size(); --i >= 0;) {
|
||||||
|
ISourceRange range = ((ISourceReference) elements.get(i)).getSourceRange();
|
||||||
|
int end = range.getStartPos() + range.getLength();
|
||||||
|
if (end <= fBeforeOffset) {
|
||||||
|
return findNewLine(range.getStartPos() + range.getLength());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int findNewLine(int pos) {
|
||||||
|
while (fBuffer.getChar(pos) != '\n') {
|
||||||
|
pos++;
|
||||||
|
}
|
||||||
|
if (fBuffer.getChar(pos) == '\r') {
|
||||||
|
pos++;
|
||||||
|
}
|
||||||
|
return pos + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getLineSeparator() {
|
||||||
|
try {
|
||||||
|
if (fBuffer instanceof IAdaptable) {
|
||||||
|
IDocument doc= (IDocument) ((IAdaptable) fBuffer).getAdapter(IDocument.class);
|
||||||
|
if (doc != null) {
|
||||||
|
String delim= doc.getLineDelimiter(0);
|
||||||
|
if (delim != null) {
|
||||||
|
return delim;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (BadLocationException e) {
|
||||||
|
}
|
||||||
|
return System.getProperty("line.separator", "\n"); //$NON-NLS-1$//$NON-NLS-2$
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,6 +62,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUsingDeclaration;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUsingDeclaration;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUsingDirective;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUsingDirective;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPConstructor;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespace;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespace;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPVariable;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPVariable;
|
||||||
import org.eclipse.cdt.core.index.IIndex;
|
import org.eclipse.cdt.core.index.IIndex;
|
||||||
|
@ -97,7 +98,7 @@ import org.eclipse.cdt.internal.ui.util.ExceptionHandler;
|
||||||
* selected name.
|
* selected name.
|
||||||
*/
|
*/
|
||||||
public class AddIncludeOnSelectionAction extends TextEditorAction {
|
public class AddIncludeOnSelectionAction extends TextEditorAction {
|
||||||
public static boolean sIsJUnitTest = false;
|
public static boolean sIsJUnitTest = false;
|
||||||
|
|
||||||
private ITranslationUnit fTu;
|
private ITranslationUnit fTu;
|
||||||
private IProject fProject;
|
private IProject fProject;
|
||||||
|
@ -105,15 +106,15 @@ public class AddIncludeOnSelectionAction extends TextEditorAction {
|
||||||
private IRequiredInclude[] fRequiredIncludes;
|
private IRequiredInclude[] fRequiredIncludes;
|
||||||
private String[] fUsingDeclarations;
|
private String[] fUsingDeclarations;
|
||||||
|
|
||||||
public AddIncludeOnSelectionAction(ITextEditor editor) {
|
public AddIncludeOnSelectionAction(ITextEditor editor) {
|
||||||
super(CEditorMessages.getBundleForConstructedKeys(), "AddIncludeOnSelection.", editor); //$NON-NLS-1$
|
super(CEditorMessages.getBundleForConstructedKeys(), "AddIncludeOnSelection.", editor); //$NON-NLS-1$
|
||||||
|
|
||||||
CUIPlugin.getDefault().getWorkbench().getHelpSystem().setHelp(this,
|
CUIPlugin.getDefault().getWorkbench().getHelpSystem().setHelp(this,
|
||||||
ICHelpContextIds.ADD_INCLUDE_ON_SELECTION_ACTION);
|
ICHelpContextIds.ADD_INCLUDE_ON_SELECTION_ACTION);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void insertInclude(IRequiredInclude[] includes, String[] usings) {
|
private void insertInclude(IRequiredInclude[] includes, String[] usings, int beforeOffset) {
|
||||||
AddIncludesOperation op= new AddIncludesOperation(fTu, includes, usings, false);
|
AddIncludesOperation op= new AddIncludesOperation(fTu, beforeOffset, includes, usings);
|
||||||
try {
|
try {
|
||||||
PlatformUI.getWorkbench().getProgressService().runInUI(
|
PlatformUI.getWorkbench().getProgressService().runInUI(
|
||||||
PlatformUI.getWorkbench().getProgressService(),
|
PlatformUI.getWorkbench().getProgressService(),
|
||||||
|
@ -125,14 +126,14 @@ public class AddIncludeOnSelectionAction extends TextEditorAction {
|
||||||
// Do nothing. Operation has been canceled.
|
// Do nothing. Operation has been canceled.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ITranslationUnit getTranslationUnit(ITextEditor editor) {
|
private static ITranslationUnit getTranslationUnit(ITextEditor editor) {
|
||||||
if (editor == null) {
|
if (editor == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return CUIPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(editor.getEditorInput());
|
return CUIPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(editor.getEditorInput());
|
||||||
}
|
}
|
||||||
|
|
||||||
private Shell getShell() {
|
private Shell getShell() {
|
||||||
return getTextEditor().getSite().getShell();
|
return getTextEditor().getSite().getShell();
|
||||||
}
|
}
|
||||||
|
@ -166,7 +167,7 @@ public class AddIncludeOnSelectionAction extends TextEditorAction {
|
||||||
}
|
}
|
||||||
|
|
||||||
final String[] lookupName = new String[1];
|
final String[] lookupName = new String[1];
|
||||||
|
|
||||||
SharedASTJob job = new SharedASTJob(CEditorMessages.AddIncludeOnSelection_label, fTu) {
|
SharedASTJob job = new SharedASTJob(CEditorMessages.AddIncludeOnSelection_label, fTu) {
|
||||||
@Override
|
@Override
|
||||||
public IStatus runOnAST(ILanguage lang, IASTTranslationUnit ast) throws CoreException {
|
public IStatus runOnAST(ILanguage lang, IASTTranslationUnit ast) throws CoreException {
|
||||||
|
@ -176,7 +177,7 @@ public class AddIncludeOnSelectionAction extends TextEditorAction {
|
||||||
};
|
};
|
||||||
job.schedule();
|
job.schedule();
|
||||||
job.join();
|
job.join();
|
||||||
|
|
||||||
if (fRequiredIncludes == null || fRequiredIncludes.length == 0 && lookupName[0].length() > 0) {
|
if (fRequiredIncludes == null || fRequiredIncludes.length == 0 && lookupName[0].length() > 0) {
|
||||||
// Try contribution from plugins.
|
// Try contribution from plugins.
|
||||||
IFunctionSummary fs = findContribution(lookupName[0]);
|
IFunctionSummary fs = findContribution(lookupName[0]);
|
||||||
|
@ -190,13 +191,13 @@ public class AddIncludeOnSelectionAction extends TextEditorAction {
|
||||||
|
|
||||||
}
|
}
|
||||||
if (fRequiredIncludes != null && fRequiredIncludes.length >= 0) {
|
if (fRequiredIncludes != null && fRequiredIncludes.length >= 0) {
|
||||||
insertInclude(fRequiredIncludes, fUsingDeclarations);
|
insertInclude(fRequiredIncludes, fUsingDeclarations, ((ITextSelection) selection).getOffset());
|
||||||
}
|
}
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extract the includes for the given selection. This can be both used to perform
|
* Extract the includes for the given selection. This can be both used to perform
|
||||||
* the work as well as being invoked when there is a change.
|
* the work as well as being invoked when there is a change.
|
||||||
|
@ -237,6 +238,14 @@ public class AddIncludeOnSelectionAction extends TextEditorAction {
|
||||||
index.findBindings(nameChars, false, filter, new NullProgressMonitor());
|
index.findBindings(nameChars, false, filter, new NullProgressMonitor());
|
||||||
|
|
||||||
for (IIndexBinding indexBinding : bindings) {
|
for (IIndexBinding indexBinding : bindings) {
|
||||||
|
// Replace ctor with the class itself.
|
||||||
|
if (indexBinding instanceof ICPPConstructor) {
|
||||||
|
try {
|
||||||
|
indexBinding = indexBinding.getOwner();
|
||||||
|
} catch (DOMException e) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
IIndexName[] definitions= null;
|
IIndexName[] definitions= null;
|
||||||
// class, struct, union, enum
|
// class, struct, union, enum
|
||||||
if (indexBinding instanceof ICompositeType || indexBinding instanceof IEnumeration) {
|
if (indexBinding instanceof ICompositeType || indexBinding instanceof IEnumeration) {
|
||||||
|
@ -255,7 +264,7 @@ public class AddIncludeOnSelectionAction extends TextEditorAction {
|
||||||
IIndexName definition = macro.getDefinition();
|
IIndexName definition = macro.getDefinition();
|
||||||
considerForInclusion(definition, macro, index, candidatesMap);
|
considerForInclusion(definition, macro, index, candidatesMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
final ArrayList<IncludeCandidate> candidates = new ArrayList<IncludeCandidate>(candidatesMap.values());
|
final ArrayList<IncludeCandidate> candidates = new ArrayList<IncludeCandidate>(candidatesMap.values());
|
||||||
if (candidates.size() > 1) {
|
if (candidates.size() > 1) {
|
||||||
if (sIsJUnitTest) {
|
if (sIsJUnitTest) {
|
||||||
|
@ -287,7 +296,7 @@ public class AddIncludeOnSelectionAction extends TextEditorAction {
|
||||||
// Decide what 'using' declaration, if any, should be added along with the include.
|
// Decide what 'using' declaration, if any, should be added along with the include.
|
||||||
String usingDeclaration = deduceUsingDeclaration(binding, indexBinding, ast);
|
String usingDeclaration = deduceUsingDeclaration(binding, indexBinding, ast);
|
||||||
if (usingDeclaration != null)
|
if (usingDeclaration != null)
|
||||||
fUsingDeclarations = new String[] { usingDeclaration };
|
fUsingDeclarations = new String[] { usingDeclaration };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -366,7 +375,7 @@ public class AddIncludeOnSelectionAction extends TextEditorAction {
|
||||||
}
|
}
|
||||||
return buf.toString();
|
return buf.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean match(IASTName name, ArrayList<String> usingChain, boolean excludeLast) {
|
private boolean match(IASTName name, ArrayList<String> usingChain, boolean excludeLast) {
|
||||||
IASTName[] names;
|
IASTName[] names;
|
||||||
if (name instanceof ICPPASTQualifiedName) {
|
if (name instanceof ICPPASTQualifiedName) {
|
||||||
|
@ -465,7 +474,7 @@ public class AddIncludeOnSelectionAction extends TextEditorAction {
|
||||||
|
|
||||||
public ITranslationUnit getTranslationUnit() {
|
public ITranslationUnit getTranslationUnit() {
|
||||||
return fTu;
|
return fTu;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
fs[0] = CHelpProviderManager.getDefault().getFunctionInfo(context, name);
|
fs[0] = CHelpProviderManager.getDefault().getFunctionInfo(context, name);
|
||||||
|
@ -514,7 +523,7 @@ public class AddIncludeOnSelectionAction extends TextEditorAction {
|
||||||
private static String getPath(IIndexFile file) throws CoreException {
|
private static String getPath(IIndexFile file) throws CoreException {
|
||||||
return file.getLocation().getURI().getPath();
|
return file.getLocation().getURI().getPath();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the RequiredInclude object to be added to the include list
|
* Returns the RequiredInclude object to be added to the include list
|
||||||
* @param path - the full path of the file to include
|
* @param path - the full path of the file to include
|
||||||
|
@ -637,7 +646,7 @@ public class AddIncludeOnSelectionAction extends TextEditorAction {
|
||||||
private final IIndexBinding binding;
|
private final IIndexBinding binding;
|
||||||
private final IRequiredInclude include;
|
private final IRequiredInclude include;
|
||||||
private final String label;
|
private final String label;
|
||||||
|
|
||||||
public IncludeCandidate(IIndexBinding binding, IRequiredInclude include) throws CoreException {
|
public IncludeCandidate(IIndexBinding binding, IRequiredInclude include) throws CoreException {
|
||||||
this.binding = binding;
|
this.binding = binding;
|
||||||
this.include = include;
|
this.include = include;
|
||||||
|
@ -680,7 +689,7 @@ public class AddIncludeOnSelectionAction extends TextEditorAction {
|
||||||
public boolean isStandard() {
|
public boolean isStandard() {
|
||||||
return isSystem;
|
return isSystem;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder buf = new StringBuilder(includeName.length() + 2);
|
StringBuilder buf = new StringBuilder(includeName.length() + 2);
|
||||||
|
|
Loading…
Add table
Reference in a new issue