mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-09 20:42:52 +07:00
file path completion on class name (ctrl+alt+space)
This commit is contained in:
+105
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* Copyright 2000-2009 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.codeInsight.intention.impl;
|
||||
|
||||
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.PsiReference;
|
||||
import com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference;
|
||||
import com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference;
|
||||
import com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReferenceOwner;
|
||||
import com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReferenceSet;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* @author spleaner
|
||||
*/
|
||||
public class ConvertAbsolutePathToRelativeIntentionAction extends PsiElementBaseIntentionAction {
|
||||
|
||||
protected boolean isConvertToRelative() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable(@NotNull Project project, Editor editor, @Nullable PsiElement element) {
|
||||
if (element == null) return false;
|
||||
final PsiFile containingFile = element.getContainingFile();
|
||||
if (containingFile == null) return false;
|
||||
|
||||
final PsiReference reference = containingFile.findReferenceAt(editor.getCaretModel().getOffset());
|
||||
final FileReference fileReference = reference == null ? null : findFileReference(reference);
|
||||
|
||||
if (fileReference != null) {
|
||||
final FileReferenceSet set = fileReference.getFileReferenceSet();
|
||||
final FileReference lastReference = set.getLastReference();
|
||||
return set.couldBeConvertedTo(isConvertToRelative()) && lastReference != null &&
|
||||
(!isConvertToRelative() && !set.isAbsolutePathReference() || isConvertToRelative() && set.isAbsolutePathReference()) &&
|
||||
lastReference.resolve() != null;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static FileReference findFileReference(@NotNull final PsiReference original) {
|
||||
if (original instanceof PsiMultiReference) {
|
||||
final PsiMultiReference multiReference = (PsiMultiReference)original;
|
||||
for (PsiReference reference : multiReference.getReferences()) {
|
||||
if (reference instanceof FileReference) {
|
||||
return (FileReference)reference;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (original instanceof FileReferenceOwner) {
|
||||
final FileReference fileReference = ((FileReferenceOwner)original).getLastFileReference();
|
||||
if (fileReference != null) {
|
||||
return fileReference;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getFamilyName() {
|
||||
return "Convert " + (isConvertToRelative() ? "absolute" : "relative") + " path to " + (isConvertToRelative() ? "relative" : "absolute");
|
||||
}
|
||||
|
||||
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
|
||||
final PsiReference reference = file.findReferenceAt(editor.getCaretModel().getOffset());
|
||||
final FileReference fileReference = reference == null ? null : findFileReference(reference);
|
||||
if (fileReference != null) {
|
||||
final FileReference lastReference = fileReference.getFileReferenceSet().getLastReference();
|
||||
if (lastReference != null) lastReference.bindToElement(lastReference.resolve(), !isConvertToRelative());
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getText() {
|
||||
return "Convert path to " + (isConvertToRelative() ? "relative" : "absolute");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean startInWriteAction() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright 2000-2009 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.codeInsight.intention.impl;
|
||||
|
||||
/**
|
||||
* @author spleaner
|
||||
*/
|
||||
public class ConvertRelativePathToAbsoluteIntentionAction extends ConvertAbsolutePathToRelativeIntentionAction {
|
||||
|
||||
@Override
|
||||
protected boolean isConvertToRelative() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+11
@@ -45,6 +45,17 @@ public class FilePathReferenceProvider extends PsiReferenceProvider {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean couldBeConvertedTo(boolean relative) {
|
||||
return !relative;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean absoluteUrlNeedsStartSlash() {
|
||||
final String s = getPathString();
|
||||
return s != null && s.length() > 0 && s.charAt(0) == '/';
|
||||
}
|
||||
|
||||
@NotNull public Collection<PsiFileSystemItem> computeDefaultContexts() {
|
||||
final Module module = ModuleUtil.findModuleForPsiElement(getElement());
|
||||
return getRoots(module, true);
|
||||
|
||||
Reference in New Issue
Block a user