1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-20 15:35:24 +02:00

Bug 416247 - Use an index-based AST when generating doxygen comments

Change-Id: Ic379ba7f51ab8379d32969856f189dacb8cb32fc
Signed-off-by: Nathan Ridge <zeratul976@hotmail.com>
This commit is contained in:
Nathan Ridge 2015-03-16 00:10:52 -04:00 committed by Gerrit Code Review @ Eclipse.org
parent ab6c210d1d
commit dc57ba11a4
2 changed files with 61 additions and 27 deletions

View file

@ -23,9 +23,9 @@ import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.IDocument;
import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.model.CoreModel; import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICProject; import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.testplugin.CProjectHelper; import org.eclipse.cdt.core.testplugin.CProjectHelper;
import org.eclipse.cdt.core.testplugin.util.TestSourceReader; import org.eclipse.cdt.core.testplugin.util.TestSourceReader;
import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.ui.CUIPlugin;
@ -689,12 +689,12 @@ public class DoxygenCCommentAutoEditStrategyTest extends AbstractAutoEditTest {
DoxygenMultilineAutoEditStrategy ds= new DoxygenMultilineAutoEditStrategy() { DoxygenMultilineAutoEditStrategy ds= new DoxygenMultilineAutoEditStrategy() {
@Override @Override
public IASTTranslationUnit getAST() { public ITranslationUnit getTranslationUnitForActiveEditor() {
final IFile file= fCProject.getProject().getFile("testContent.cpp"); final IFile file= fCProject.getProject().getFile("testContent.cpp");
try { try {
TestSourceReader.createFile(fCProject.getProject(), "testContent.cpp", doc.get()); TestSourceReader.createFile(fCProject.getProject(), "testContent.cpp", doc.get());
String id = CoreModel.getRegistedContentTypeId(file.getProject(), file.getName()); String id = CoreModel.getRegistedContentTypeId(file.getProject(), file.getName());
return new TranslationUnit(fCProject, file, id).getAST(); return new TranslationUnit(fCProject, file, id);
} catch(CoreException ce) { } catch(CoreException ce) {
assertTrue("Could not get test content AST", false); assertTrue("Could not get test content AST", false);
return null; return null;

View file

@ -32,12 +32,14 @@ import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI; import org.eclipse.ui.PlatformUI;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ast.ASTVisitor; import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration; import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTNodeLocation; import org.eclipse.cdt.core.dom.ast.IASTNodeLocation;
import org.eclipse.cdt.core.dom.ast.IASTNodeSelector; import org.eclipse.cdt.core.dom.ast.IASTNodeSelector;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.model.CModelException; import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.ITranslationUnit; import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.ui.CUIPlugin;
@ -148,7 +150,18 @@ public class DefaultMultilineCommentAutoEditStrategy implements IAutoEditStrateg
// as we are auto-closing, the comment becomes eligible for auto-doc'ing // as we are auto-closing, the comment becomes eligible for auto-doc'ing
IASTDeclaration dec= null; IASTDeclaration dec= null;
IASTTranslationUnit ast= getAST(); IIndex index = null;
ITranslationUnit unit = getTranslationUnitForActiveEditor();
if (unit != null) {
index = CCorePlugin.getIndexManager().getIndex(unit.getCProject());
try {
index.acquireReadLock();
} catch (InterruptedException e) {
index = null;
}
}
try {
IASTTranslationUnit ast = getAST(unit, index);
if (ast != null) { if (ast != null) {
dec= findFollowingDeclaration(ast, offset); dec= findFollowingDeclaration(ast, offset);
@ -166,9 +179,15 @@ public class DefaultMultilineCommentAutoEditStrategy implements IAutoEditStrateg
StringBuilder content= customizeAfterNewLineForDeclaration(doc, dec, partition); StringBuilder content= customizeAfterNewLineForDeclaration(doc, dec, partition);
buf.append(indent(content, indentation + MULTILINE_MID, lineDelim)); buf.append(indent(content, indentation + MULTILINE_MID, lineDelim));
} }
} finally {
if (index != null) {
index.releaseReadLock();
}
}
} catch(BadLocationException ble) { } catch(BadLocationException ble) {
ble.printStackTrace(); CUIPlugin.log(ble);
} catch(CoreException e) {
CUIPlugin.log(e);
} }
} }
@ -241,14 +260,22 @@ public class DefaultMultilineCommentAutoEditStrategy implements IAutoEditStrateg
} }
/** /**
* @return the AST unit for the active editor, or <code>null</code> if there is no active editor, or * @return the AST unit for the active editor, not based on an index, or <code>null</code> if there
* the AST could not be obtained. * is no active editor, or the AST could not be obtained.
*/ */
public IASTTranslationUnit getAST() { public IASTTranslationUnit getAST() {
final ITranslationUnit unit= getTranslationUnit(); return getAST(getTranslationUnitForActiveEditor(), null);
}
/**
* @return the AST for the given translation unit, based on the given index, or <code>null</code> if
* the translation unit is <code>null</code>, or the AST could not be obtained.
* @since 5.10
*/
public IASTTranslationUnit getAST(ITranslationUnit unit, IIndex index) {
try { try {
if (unit != null) { if (unit != null) {
IASTTranslationUnit ast= unit.getAST(null, ITranslationUnit.AST_SKIP_ALL_HEADERS); IASTTranslationUnit ast= unit.getAST(index, ITranslationUnit.AST_SKIP_ALL_HEADERS);
return ast; return ast;
} }
} catch (CModelException e) { } catch (CModelException e) {
@ -297,10 +324,9 @@ public class DefaultMultilineCommentAutoEditStrategy implements IAutoEditStrateg
/** /**
* @return the ITranslationUnit for the active editor, or null if no active * @return the ITranslationUnit for the active editor, or null if no active
* editor could be found. * editor could be found.
* @deprecated use getTranslationUnitForActiveEditor() instead
*/ */
/* @Deprecated
* Cloned from JDT
*/
protected static ITranslationUnit getTranslationUnit() { protected static ITranslationUnit getTranslationUnit() {
IWorkbenchWindow window= PlatformUI.getWorkbench().getActiveWorkbenchWindow(); IWorkbenchWindow window= PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (window == null) if (window == null)
@ -322,6 +348,14 @@ public class DefaultMultilineCommentAutoEditStrategy implements IAutoEditStrateg
return unit; return unit;
} }
/**
* Same as above, but nonstatic so derived classes can override it.
* @since 5.10
*/
protected ITranslationUnit getTranslationUnitForActiveEditor() {
return getTranslationUnit();
}
/** /**
* Returns a new buffer with the specified indent string inserted at the beginning * Returns a new buffer with the specified indent string inserted at the beginning
* of each line in the specified input buffer * of each line in the specified input buffer