本文整理汇总了Java中org.jetbrains.plugins.groovy.lang.psi.GroovyFile.removeImport方法的典型用法代码示例。如果您正苦于以下问题:Java GroovyFile.removeImport方法的具体用法?Java GroovyFile.removeImport怎么用?Java GroovyFile.removeImport使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.jetbrains.plugins.groovy.lang.psi.GroovyFile
的用法示例。
在下文中一共展示了GroovyFile.removeImport方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: removeRedundantImports
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile; //导入方法依赖的package包/类
@Override
public void removeRedundantImports(PsiFile targetClassFile) {
if (targetClassFile instanceof GroovyFile) {
GroovyFile file = (GroovyFile)targetClassFile;
final Set<GrImportStatement> usedImports = GroovyImportUtil.findUsedImports(file);
final List<GrImportStatement> validImports = org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil.getValidImportStatements(file);
for (GrImportStatement importStatement : validImports) {
if (!usedImports.contains(importStatement)) {
file.removeImport(importStatement);
}
}
}
}
示例2: run
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile; //导入方法依赖的package包/类
@Override
public void run() {
if (!(myFile instanceof GroovyFile)) return;
GroovyFile file = ((GroovyFile)myFile);
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(file.getProject());
final Document document = documentManager.getDocument(file);
if (document != null) {
documentManager.commitDocument(document);
}
final Set<String> simplyImportedClasses = new LinkedHashSet<String>();
final Set<String> staticallyImportedMembers = new LinkedHashSet<String>();
final Set<GrImportStatement> usedImports = new HashSet<GrImportStatement>();
final Set<GrImportStatement> unresolvedOnDemandImports = new HashSet<GrImportStatement>();
final Set<String> implicitlyImportedClasses = new LinkedHashSet<String>();
final Set<String> innerClasses = new HashSet<String>();
Map<String, String> aliasImported = ContainerUtil.newHashMap();
Map<String, String> annotatedImports = ContainerUtil.newHashMap();
GroovyImportUtil.processFile(myFile, simplyImportedClasses, staticallyImportedMembers, usedImports, unresolvedOnDemandImports,
implicitlyImportedClasses, innerClasses,
aliasImported, annotatedImports);
final List<GrImportStatement> oldImports = PsiUtil.getValidImportStatements(file);
if (myRemoveUnusedOnly) {
for (GrImportStatement oldImport : oldImports) {
if (!usedImports.contains(oldImport)) {
file.removeImport(oldImport);
}
}
return;
}
// Add new import statements
GrImportStatement[] newImports =
prepare(usedImports, simplyImportedClasses, staticallyImportedMembers, implicitlyImportedClasses, innerClasses, aliasImported,
annotatedImports, unresolvedOnDemandImports);
if (oldImports.isEmpty() && newImports.length == 0 && aliasImported.isEmpty()) {
return;
}
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(file.getProject());
GroovyFile tempFile = factory.createGroovyFile("", false, null);
for (GrImportStatement newImport : newImports) {
tempFile.addImport(newImport);
}
if (!oldImports.isEmpty()) {
final int startOffset = oldImports.get(0).getTextRange().getStartOffset();
final int endOffset = oldImports.get(oldImports.size() - 1).getTextRange().getEndOffset();
String oldText = oldImports.isEmpty() ? "" : myFile.getText().substring(startOffset, endOffset);
if (tempFile.getText().trim().equals(oldText)) {
return;
}
}
for (GrImportStatement statement : tempFile.getImportStatements()) {
file.addImport(statement);
}
for (GrImportStatement importStatement : oldImports) {
file.removeImport(importStatement);
}
}