本文整理汇总了Java中org.jetbrains.plugins.groovy.lang.psi.GroovyFile.getImportStatements方法的典型用法代码示例。如果您正苦于以下问题:Java GroovyFile.getImportStatements方法的具体用法?Java GroovyFile.getImportStatements怎么用?Java GroovyFile.getImportStatements使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.jetbrains.plugins.groovy.lang.psi.GroovyFile
的用法示例。
在下文中一共展示了GroovyFile.getImportStatements方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: collectAliases
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile; //导入方法依赖的package包/类
@NotNull
private static MultiMap<String, String> collectAliases(@NotNull GroovyFile file) {
MultiMap<String, String> aliases = MultiMap.createSet();
for (GrImportStatement anImport : file.getImportStatements()) {
if (anImport.isAliasedImport()) {
final GrCodeReferenceElement importReference = anImport.getImportReference();
if (importReference != null) {
final String refName = importReference.getReferenceName();
if (refName != null) {
aliases.putValue(refName, anImport.getImportedName());
}
}
}
}
return aliases;
}
示例2: timeToOptimizeImports
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile; //导入方法依赖的package包/类
private boolean timeToOptimizeImports(GroovyFile myFile, Editor editor) {
if (!CodeInsightSettings.getInstance().OPTIMIZE_IMPORTS_ON_THE_FLY) return false;
if (onTheFly && editor != null) {
// if we stand inside import statements, do not optimize
final VirtualFile vfile = myFile.getVirtualFile();
if (vfile != null && ProjectRootManager.getInstance(myFile.getProject()).getFileIndex().isInSource(vfile)) {
final GrImportStatement[] imports = myFile.getImportStatements();
if (imports.length > 0) {
final int offset = editor.getCaretModel().getOffset();
if (imports[0].getTextRange().getStartOffset() <= offset && offset <= imports[imports.length - 1].getTextRange().getEndOffset()) {
return false;
}
}
}
}
DaemonCodeAnalyzerImpl codeAnalyzer = (DaemonCodeAnalyzerImpl)DaemonCodeAnalyzer.getInstance(myFile.getProject());
if (!codeAnalyzer.isHighlightingAvailable(myFile)) return false;
if (!codeAnalyzer.isErrorAnalyzingFinished(myFile)) return false;
Document myDocument = PsiDocumentManager.getInstance(myFile.getProject()).getDocument(myFile);
boolean errors = containsErrorsPreventingOptimize(myFile, myDocument);
return !errors && DaemonListeners.canChangeFileSilently(myFile);
}
示例3: containsErrorsPreventingOptimize
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile; //导入方法依赖的package包/类
private boolean containsErrorsPreventingOptimize(GroovyFile myFile, Document myDocument) {
// ignore unresolved imports errors
final TextRange ignoreRange;
final GrImportStatement[] imports = myFile.getImportStatements();
if (imports.length != 0) {
final int start = imports[0].getTextRange().getStartOffset();
final int end = imports[imports.length - 1].getTextRange().getEndOffset();
ignoreRange = new TextRange(start, end);
} else {
ignoreRange = TextRange.EMPTY_RANGE;
}
return !DaemonCodeAnalyzerEx
.processHighlights(myDocument, myFile.getProject(), HighlightSeverity.ERROR, 0, myDocument.getTextLength(), new Processor<HighlightInfo>() {
@Override
public boolean process(HighlightInfo error) {
int infoStart = error.getActualStartOffset();
int infoEnd = error.getActualEndOffset();
return ignoreRange.containsRange(infoStart, infoEnd) && error.type.equals(HighlightInfoType.WRONG_REF);
}
});
}
示例4: findAliasedName
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile; //导入方法依赖的package包/类
private static String findAliasedName(Map<GroovyFile, String> map, GroovyFile containingFile, PsiElement elementToResolve) {
final String s = map.get(containingFile);
if (s != null) return s;
final GrImportStatement[] imports = containingFile.getImportStatements();
final PsiManager manager = elementToResolve.getManager();
for (GrImportStatement anImport : imports) {
if (anImport.isAliasedImport()) {
final ResolverProcessor processor = getProcessor(elementToResolve, containingFile);
anImport.processDeclarations(processor, ResolveState.initial(), null, containingFile);
final GroovyResolveResult[] results = processor.getCandidates();
for (GroovyResolveResult result : results) {
if (manager.areElementsEquivalent(elementToResolve, result.getElement())) {
final String importedName = anImport.getImportedName();
if (importedName != null) {
map.put(containingFile, importedName);
return importedName;
}
}
}
}
}
map.put(containingFile, EMPTY_ALIAS);
return EMPTY_ALIAS;
}
示例5: stripImports
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile; //导入方法依赖的package包/类
private static String stripImports(String text, GroovyFile toEval) {
GrImportStatement[] imports = toEval.getImportStatements();
for (int i = imports.length - 1; i >= 0; i--) {
TextRange range = imports[i].getTextRange();
text = text.substring(0, range.getStartOffset()) + text.substring(range.getEndOffset(), text.length());
}
return StringUtil.escapeStringCharacters(text);
}
示例6: importAlreadyExists
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile; //导入方法依赖的package包/类
private static boolean importAlreadyExists(final PsiMember member, final GroovyFile file, final PsiElement place) {
final PsiManager manager = file.getManager();
PsiScopeProcessor processor = new PsiScopeProcessor() {
@Override
public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) {
return !manager.areElementsEquivalent(element, member);
}
@Override
public <T> T getHint(@NotNull Key<T> hintKey) {
return null;
}
@Override
public void handleEvent(@NotNull Event event, Object associated) {
}
};
boolean skipStaticImports = member instanceof PsiClass;
final GrImportStatement[] imports = file.getImportStatements();
final ResolveState initial = ResolveState.initial();
for (GrImportStatement anImport : imports) {
if (skipStaticImports == anImport.isStatic()) continue;
if (!anImport.processDeclarations(processor, initial, null, place)) return true;
}
return false;
}
示例7: getAnchorToInsertImportAfter
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile; //导入方法依赖的package包/类
@Nullable
private PsiElement getAnchorToInsertImportAfter(@NotNull GroovyFile psiFile, @NotNull GrImportStatement statement) {
final GroovyCodeStyleSettings settings = CodeStyleSettingsManager.getInstance(psiFile.getProject()).getCurrentSettings().getCustomSettings(
GroovyCodeStyleSettings.class);
final PackageEntryTable layoutTable = settings.IMPORT_LAYOUT_TABLE;
final PackageEntry[] entries = layoutTable.getEntries();
GrImportStatement[] importStatements = psiFile.getImportStatements();
if (importStatements.length == 0) {
final GrPackageDefinition definition = psiFile.getPackageDefinition();
if (definition != null) {
return definition;
}
return getShellComment(psiFile);
}
final Comparator<GrImportStatement> comparator = GroovyImportOptimizer.getComparator(settings);
final int idx = getPackageEntryIdx(entries, statement);
PsiElement anchor = null;
for (GrImportStatement importStatement : importStatements) {
final int i = getPackageEntryIdx(entries, importStatement);
if (i < idx) {
anchor = importStatement;
}
else if (i > idx) {
break;
}
else if (comparator.compare(statement, importStatement) > 0) {
anchor = importStatement;
}
else {
break;
}
}
if (anchor == null) anchor = psiFile.getPackageDefinition();
if (anchor == null) anchor = getShellComment(psiFile);
if (anchor == null && importStatements.length > 0) anchor = importStatements[0].getPrevSibling();
return anchor;
}
示例8: 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);
}
}
示例9: generateNewScript
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile; //导入方法依赖的package包/类
private static GroovyFile generateNewScript(GroovyFile file, PsiPackage newPackage) {
for (GrImportStatement importStatement : file.getImportStatements()) {
importStatement.delete();
}
final GroovyFile newFile = GroovyPsiElementFactory.getInstance(file.getProject()).createGroovyFile("", true, null);
newFile.addRange(file.getFirstChild(), file.getLastChild());
final PsiClass[] newFileClasses = newFile.getClasses();
for (PsiClass psiClass : newFileClasses) {
if (psiClass instanceof GroovyScriptClass) continue;
final GrDocComment docComment = GrDocCommentUtil.findDocComment((GrDocCommentOwner)psiClass);
if (docComment != null) docComment.delete();
psiClass.delete();
}
final GrPackageDefinition packageDefinition = newFile.getPackageDefinition();
if (packageDefinition != null) packageDefinition.delete();
PsiElement cur = newFile.getFirstChild();
while (cur != null && PsiImplUtil.isWhiteSpaceOrNls(cur)) {
cur = cur.getNextSibling();
}
if (cur != null && cur != newFile.getFirstChild()) {
cur = cur.getPrevSibling();
newFile.deleteChildRange(newFile.getFirstChild(), cur);
}
cur = newFile.getLastChild();
while (cur != null && PsiImplUtil.isWhiteSpaceOrNls(cur)) {
cur = cur.getPrevSibling();
}
if (cur != null && cur != newFile.getLastChild()) {
cur = cur.getNextSibling();
newFile.deleteChildRange(cur, newFile.getLastChild());
}
newFile.setName(file.getName());
setPackageDefinition(file, newFile, newPackage.getQualifiedName());
GroovyChangeContextUtil.decodeContextInfo(newFile, null, null);
return newFile;
}