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:
parent
ab6c210d1d
commit
dc57ba11a4
2 changed files with 61 additions and 27 deletions
|
@ -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;
|
||||||
|
|
|
@ -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,27 +150,44 @@ 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 (ast != null) {
|
if (unit != null) {
|
||||||
dec= findFollowingDeclaration(ast, offset);
|
index = CCorePlugin.getIndexManager().getIndex(unit.getCProject());
|
||||||
if (dec == null) {
|
try {
|
||||||
IASTNodeSelector ans= ast.getNodeSelector(ast.getFilePath());
|
index.acquireReadLock();
|
||||||
IASTNode node= ans.findEnclosingNode(offset, 0);
|
} catch (InterruptedException e) {
|
||||||
if (node instanceof IASTDeclaration) {
|
index = null;
|
||||||
dec= (IASTDeclaration) node;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
IASTTranslationUnit ast = getAST(unit, index);
|
||||||
|
|
||||||
if (dec != null) {
|
if (ast != null) {
|
||||||
ITypedRegion partition= TextUtilities.getPartition(doc, ICPartitions.C_PARTITIONING /* this! */, offset, false);
|
dec= findFollowingDeclaration(ast, offset);
|
||||||
StringBuilder content= customizeAfterNewLineForDeclaration(doc, dec, partition);
|
if (dec == null) {
|
||||||
buf.append(indent(content, indentation + MULTILINE_MID, lineDelim));
|
IASTNodeSelector ans= ast.getNodeSelector(ast.getFilePath());
|
||||||
|
IASTNode node= ans.findEnclosingNode(offset, 0);
|
||||||
|
if (node instanceof IASTDeclaration) {
|
||||||
|
dec= (IASTDeclaration) node;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dec != null) {
|
||||||
|
ITypedRegion partition= TextUtilities.getPartition(doc, ICPartitions.C_PARTITIONING /* this! */, offset, false);
|
||||||
|
StringBuilder content= customizeAfterNewLineForDeclaration(doc, dec, partition);
|
||||||
|
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
|
||||||
|
|
Loading…
Add table
Reference in a new issue