本文整理匯總了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);
}
}