本文整理汇总了Java中com.intellij.codeInsight.completion.InsertionContext.getProject方法的典型用法代码示例。如果您正苦于以下问题:Java InsertionContext.getProject方法的具体用法?Java InsertionContext.getProject怎么用?Java InsertionContext.getProject使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.intellij.codeInsight.completion.InsertionContext
的用法示例。
在下文中一共展示了InsertionContext.getProject方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: shortenParamterReferences
import com.intellij.codeInsight.completion.InsertionContext; //导入方法依赖的package包/类
private static int shortenParamterReferences(InsertionContext context, int startOffset, PsiMethod method, StringBuffer buffer) {
Document document = context.getEditor().getDocument();
int offset = startOffset + method.getName().length();
String paramText = buffer.toString();
document.insertString(offset, paramText);
int endOffset = offset + paramText.length();
final Project project = context.getProject();
PsiDocumentManager.getInstance(project).commitDocument(document);
PsiReference ref = context.getFile().findReferenceAt(startOffset);
if (ref instanceof GrDocMethodReference) {
GrDocMethodReference methodReference = (GrDocMethodReference) ref;
GrDocMethodParams list = methodReference.getParameterList();
for (GrDocMethodParameter parameter : list.getParameters()) {
JavaCodeStyleManager.getInstance(project).shortenClassReferences(parameter);
}
endOffset = methodReference.getTextRange().getEndOffset() + 1;
}
return endOffset;
}
示例2: handleInsert
import com.intellij.codeInsight.completion.InsertionContext; //导入方法依赖的package包/类
@Override
public void handleInsert(InsertionContext context) {
final Document document = context.getEditor().getDocument();
document.replaceString(context.getStartOffset(), context.getTailOffset(), myCanonicalText + DOT_CLASS);
final Project project = context.getProject();
PsiDocumentManager.getInstance(project).commitDocument(document);
JavaCodeStyleManager.getInstance(project).shortenClassReferences(context.getFile(), context.getStartOffset(), context.getTailOffset());
}
示例3: handleInsert
import com.intellij.codeInsight.completion.InsertionContext; //导入方法依赖的package包/类
@Override
public void handleInsert(final InsertionContext context) {
final RangeMarker rangeMarker = context.getDocument().createRangeMarker(context.getStartOffset(), context.getStartOffset());
getDelegate().handleInsert(context);
final PsiFile file = context.getFile();
((PsiJavaFile)file).importClass(myPsiClass);
final PsiElement caretElement = file.findElementAt(context.getEditor().getCaretModel().getOffset());
if (caretElement == null) {
log.error("element on caret position MUST BE not null");
return;
}
final PsiStatement statement = (PsiStatement) caretElement.getPrevSibling();
final PsiCodeBlock codeBlock = PsiTreeUtil.getParentOfType(statement, PsiCodeBlock.class);
if (codeBlock == null) {
log.error("code block MUST BE not null");
return;
}
final Project project = context.getProject();
final Ref<PsiElement> insertedStatementRef = Ref.create();
final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(project);
context.commitDocument();
new WriteCommandAction.Simple(project, file) {
@Override
protected void run() throws Throwable {
final PsiStatement statementFromText = elementFactory.createStatementFromText(String.format("%s %s = null;", myPsiClass.getName(), myNewVarName), null);
insertedStatementRef.set(codeBlock.addBefore(statementFromText, statement));
}
}.execute();
final PsiLiteralExpression nullKeyword = findNull(insertedStatementRef.get());
PsiDocumentManager.getInstance(context.getProject()).doPostponedOperationsAndUnblockDocument(context.getDocument());
context.getDocument().insertString(rangeMarker.getStartOffset(), myNewVarName + ".");
context.commitDocument();
final int offset = nullKeyword.getTextOffset();
final int endOffset = offset + nullKeyword.getTextLength();
context.getEditor().getSelectionModel().setSelection(offset, endOffset);
context.getEditor().getCaretModel().moveToOffset(offset);
}
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:38,代码来源:ChainCompletionNewVariableLookupElement.java
示例4: handleInsert
import com.intellij.codeInsight.completion.InsertionContext; //导入方法依赖的package包/类
@Override
public void handleInsert(final InsertionContext context) {
LookupElement item = getDelegate();
Project project = context.getProject();
Editor editor = context.getEditor();
PsiDocumentManager.getInstance(project).commitAllDocuments();
TextRange range = myState.getCurrentVariableRange();
final TemplateLookupSelectionHandler handler = item.getUserData(TemplateLookupSelectionHandler.KEY_IN_LOOKUP_ITEM);
if (handler != null && range != null) {
handler.itemSelected(item, context.getFile(), context.getDocument(), range.getStartOffset(), range.getEndOffset());
}
else {
super.handleInsert(context);
}
if (context.getCompletionChar() == '.') {
EditorModificationUtil.insertStringAtCaret(editor, ".");
AutoPopupController.getInstance(project).autoPopupMemberLookup(editor, null);
return;
}
if (!myState.isFinished()) {
myState.calcResults(true);
}
myState.nextTab();
}
示例5: handleInsert
import com.intellij.codeInsight.completion.InsertionContext; //导入方法依赖的package包/类
@Override
public void handleInsert(final InsertionContext context) {
LookupElement item = getDelegate();
Project project = context.getProject();
Editor editor = context.getEditor();
PsiDocumentManager.getInstance(project).commitAllDocuments();
TextRange range = myState.getCurrentVariableRange();
final TemplateLookupSelectionHandler handler =
item instanceof LookupItem ? ((LookupItem<?>)item).getAttribute(TemplateLookupSelectionHandler.KEY_IN_LOOKUP_ITEM) : null;
if (handler != null && range != null) {
handler.itemSelected(item, context.getFile(), context.getDocument(), range.getStartOffset(), range.getEndOffset());
}
else {
super.handleInsert(context);
}
if (context.getCompletionChar() == '.') {
EditorModificationUtil.insertStringAtCaret(editor, ".");
AutoPopupController.getInstance(project).autoPopupMemberLookup(editor, null);
return;
}
if (!myState.isFinished()) {
myState.calcResults(true);
}
myState.nextTab();
}
示例6: handleInsert
import com.intellij.codeInsight.completion.InsertionContext; //导入方法依赖的package包/类
@Override
public void handleInsert(InsertionContext context) {
PsiElement element = context.getFile().getViewProvider().findElementAt(context.getSelectionEndOffset());
element = PsiTreeUtil.getParentOfType(element, StringLiteralExpression.class);
if (element == null) {
return;
}
String value = ((StringLiteralExpression) element).getContents();
if (value.contains("/")) {
value = value.substring(value.lastIndexOf('/') + 1);
}
if (!myName.equals(value)) {
String filename = myFile.getName();
if (!filename.equals(value)) {
return;
}
}
MethodReference reference = PsiTreeUtil.getParentOfType(element, MethodReference.class);
if (reference != null) {
reference.putUserData(ViewsUtil.RENDER_VIEW, ((StringLiteralExpression) element).getContents());
reference.putUserData(ViewsUtil.RENDER_VIEW_FILE, myFile);
}
ArrayList<String> params = ViewsUtil.getViewVariables(myFile);
if (params.size() > 0) {
ParameterList parameterList = (ParameterList) element.getParent();
if (parameterList.getParameters().length == 1) {
Project project = context.getProject();
Template template = TemplateManager.getInstance(project).createTemplate("", "");
template.addTextSegment(", [");
boolean addComma = false;
for (String param : params) {
String variableName = "$" + param.toUpperCase() + "$";
if (addComma) {
template.addTextSegment(", ");
}
template.addTextSegment("'" + param + "' => ");
template.addVariable(variableName, "", "\"$" + param + "\"", true);
template.addVariableSegment(variableName);
addComma = true;
}
template.addTextSegment("]");
int offset = parameterList.getParameters()[0].getTextRange().getEndOffset();
context.getEditor().getCaretModel().moveToOffset(offset);
TemplateManager.getInstance(project).startTemplate(context.getEditor(), template);
}
}
}
示例7: addTypeAndClassifierAndVersion
import com.intellij.codeInsight.completion.InsertionContext; //导入方法依赖的package包/类
public static void addTypeAndClassifierAndVersion(@NotNull InsertionContext context,
@NotNull MavenDomDependency dependency,
@NotNull String groupId, @NotNull String artifactId) {
if (!StringUtil.isEmpty(dependency.getVersion().getStringValue())) return;
Project project = context.getProject();
if (!isInsideManagedDependency(dependency)) {
MavenDomProjectModel model = DomUtil.<MavenDomProjectModel>getFileElement(dependency).getRootElement();
MavenDomDependency managedDependency = findManagedDependency(model, project, groupId, artifactId);
if (managedDependency != null) {
if (dependency.getClassifier().getXmlTag() == null && dependency.getType().getXmlTag() == null) {
String classifier = managedDependency.getClassifier().getRawText();
if (StringUtil.isNotEmpty(classifier)) {
dependency.getClassifier().setStringValue(classifier);
}
String type = managedDependency.getType().getRawText();
if (StringUtil.isNotEmpty(type)) {
dependency.getType().setStringValue(type);
}
}
return;
}
}
MavenProjectIndicesManager manager = MavenProjectIndicesManager.getInstance(project);
Set<String> versions = manager.getVersions(groupId, artifactId);
if (versions.size() == 1) {
dependency.getVersion().setStringValue(ContainerUtil.getFirstItem(versions));
return;
}
dependency.getVersion().setStringValue("");
int versionPosition = dependency.getVersion().getXmlTag().getValue().getTextRange().getStartOffset();
context.getEditor().getCaretModel().moveToOffset(versionPosition);
if (versions.size() > 0) {
invokeCompletion(context, CompletionType.BASIC);
}
}
示例8: getParameterInfos
import com.intellij.codeInsight.completion.InsertionContext; //导入方法依赖的package包/类
@Nullable
@Override
protected List<ClosureParameterInfo> getParameterInfos(InsertionContext context,
PsiMethod method,
PsiSubstitutor substitutor,
PsiElement place) {
final String name = method.getName();
if (!"eachWithIndex".equals(name)) return null;
if (method instanceof GrGdkMethod) method = ((GrGdkMethod)method).getStaticMethod();
final PsiClass containingClass = method.getContainingClass();
if (containingClass == null) return null;
final String qname = containingClass.getQualifiedName();
if (!GroovyCommonClassNames.DEFAULT_GROOVY_METHODS.equals(qname)) return null;
final PsiParameter[] parameters = method.getParameterList().getParameters();
if (parameters.length != 2) return null;
final PsiType type = parameters[0].getType();
final PsiType collection = substitutor.substitute(type);
final PsiType iterable = getIteratedType(place, collection);
if (iterable != null) {
return Arrays.asList(
new ClosureParameterInfo(iterable.getCanonicalText(), "entry"),
new ClosureParameterInfo("int", "i")
);
}
if (InheritanceUtil.isInheritor(collection, CommonClassNames.JAVA_UTIL_MAP)) {
final PsiType[] typeParams = ((PsiClassType)collection).getParameters();
final Project project = context.getProject();
final PsiClass entry = JavaPsiFacade.getInstance(project).findClass(CommonClassNames.JAVA_UTIL_MAP_ENTRY, place.getResolveScope());
if (entry == null) return null;
final PsiClassType entryType = JavaPsiFacade.getElementFactory(project).createType(entry, typeParams);
return Arrays.asList(new ClosureParameterInfo(entryType.getCanonicalText(), "entry"), new ClosureParameterInfo("int", "i"));
}
return Arrays.asList(new ClosureParameterInfo(collection.getCanonicalText(), "entry"), new ClosureParameterInfo("int", "i"));
}
示例9: handleInsert
import com.intellij.codeInsight.completion.InsertionContext; //导入方法依赖的package包/类
@Override
public void handleInsert(InsertionContext context, LookupElement item) {
if (!(item.getObject() instanceof PsiMethod)) {
return;
}
PsiDocumentManager.getInstance(context.getProject()).commitDocument(context.getEditor().getDocument());
final Editor editor = context.getEditor();
final PsiMethod method = (PsiMethod)item.getObject();
final PsiParameter[] parameters = method.getParameterList().getParameters();
final StringBuilder buffer = new StringBuilder();
final CharSequence chars = editor.getDocument().getCharsSequence();
int endOffset = editor.getCaretModel().getOffset();
final Project project = context.getProject();
int afterSharp = CharArrayUtil.shiftBackwardUntil(chars, endOffset - 1, "#") + 1;
int signatureOffset = afterSharp;
PsiElement element = context.getFile().findElementAt(signatureOffset - 1);
final CodeStyleSettings styleSettings = CodeStyleSettingsManager.getSettings(element.getProject());
PsiDocTag tag = PsiTreeUtil.getParentOfType(element, PsiDocTag.class);
if (context.getCompletionChar() == Lookup.REPLACE_SELECT_CHAR) {
final PsiDocTagValue value = tag.getValueElement();
endOffset = value.getTextRange().getEndOffset();
}
editor.getDocument().deleteString(afterSharp, endOffset);
editor.getCaretModel().moveToOffset(signatureOffset);
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
editor.getSelectionModel().removeSelection();
buffer.append(method.getName()).append("(");
final int afterParenth = afterSharp + buffer.length();
for (int i = 0; i < parameters.length; i++) {
final PsiType type = TypeConversionUtil.erasure(parameters[i].getType());
buffer.append(type.getCanonicalText());
if (i < parameters.length - 1) {
buffer.append(",");
if (styleSettings.SPACE_AFTER_COMMA) buffer.append(" ");
}
}
buffer.append(")");
if (!(tag instanceof PsiInlineDocTag)) {
buffer.append(" ");
}
else {
final int currentOffset = editor.getCaretModel().getOffset();
if (chars.charAt(currentOffset) == '}') {
afterSharp++;
}
else {
buffer.append("} ");
}
}
String insertString = buffer.toString();
EditorModificationUtil.insertStringAtCaret(editor, insertString);
editor.getCaretModel().moveToOffset(afterSharp + buffer.length());
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
shortenReferences(project, editor, context, afterParenth);
}
示例10: getParameterInfos
import com.intellij.codeInsight.completion.InsertionContext; //导入方法依赖的package包/类
@Nullable
@Override
protected List<ClosureParameterInfo> getParameterInfos(InsertionContext context,
PsiMethod method,
PsiSubstitutor substitutor,
Document document,
int offset,
PsiElement parent) {
final String name = method.getName();
if (!"eachWithIndex".equals(name)) return null;
if (method instanceof GrGdkMethod) method = ((GrGdkMethod)method).getStaticMethod();
final PsiClass containingClass = method.getContainingClass();
if (containingClass == null) return null;
final String qname = containingClass.getQualifiedName();
if (!GroovyCommonClassNames.DEFAULT_GROOVY_METHODS.equals(qname)) return null;
final PsiParameter[] parameters = method.getParameterList().getParameters();
if (parameters.length != 2) return null;
final PsiType type = parameters[0].getType();
final PsiType collection = substitutor.substitute(type);
final PsiType iterable = getIteratedType(parent, collection);
if (iterable != null) {
return Arrays.asList(
new ClosureParameterInfo(iterable.getCanonicalText(), "entry"),
new ClosureParameterInfo("int", "i")
);
}
if (InheritanceUtil.isInheritor(collection, CommonClassNames.JAVA_UTIL_MAP)) {
final PsiType[] typeParams = ((PsiClassType)collection).getParameters();
final Project project = context.getProject();
final PsiClass entry = JavaPsiFacade.getInstance(project).findClass(CommonClassNames.JAVA_UTIL_MAP_ENTRY, parent.getResolveScope());
if (entry == null) return null;
final PsiClassType entryType = JavaPsiFacade.getElementFactory(project).createType(entry, typeParams);
return Arrays.asList(new ClosureParameterInfo(entryType.getCanonicalText(), "entry"), new ClosureParameterInfo("int", "i"));
}
return Arrays.asList(new ClosureParameterInfo(collection.getCanonicalText(), "entry"), new ClosureParameterInfo("int", "i"));
}
示例11: handleInsert
import com.intellij.codeInsight.completion.InsertionContext; //导入方法依赖的package包/类
public void handleInsert(InsertionContext context, LookupElement item) {
if (!(item.getObject() instanceof PsiMethod)) {
return;
}
PsiDocumentManager.getInstance(context.getProject()).commitDocument(context.getEditor().getDocument());
final Editor editor = context.getEditor();
final PsiMethod method = (PsiMethod)item.getObject();
final PsiParameter[] parameters = method.getParameterList().getParameters();
final StringBuilder buffer = new StringBuilder();
final CharSequence chars = editor.getDocument().getCharsSequence();
int endOffset = editor.getCaretModel().getOffset();
final Project project = context.getProject();
int afterSharp = CharArrayUtil.shiftBackwardUntil(chars, endOffset - 1, "#") + 1;
int signatureOffset = afterSharp;
PsiElement element = context.getFile().findElementAt(signatureOffset - 1);
final CodeStyleSettings styleSettings = CodeStyleSettingsManager.getSettings(element.getProject());
PsiDocTag tag = PsiTreeUtil.getParentOfType(element, PsiDocTag.class);
if (context.getCompletionChar() == Lookup.REPLACE_SELECT_CHAR) {
final PsiDocTagValue value = tag.getValueElement();
endOffset = value.getTextRange().getEndOffset();
}
editor.getDocument().deleteString(afterSharp, endOffset);
editor.getCaretModel().moveToOffset(signatureOffset);
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
editor.getSelectionModel().removeSelection();
buffer.append(method.getName()).append("(");
final int afterParenth = afterSharp + buffer.length();
for (int i = 0; i < parameters.length; i++) {
final PsiType type = TypeConversionUtil.erasure(parameters[i].getType());
buffer.append(type.getCanonicalText());
if (i < parameters.length - 1) {
buffer.append(",");
if (styleSettings.SPACE_AFTER_COMMA) buffer.append(" ");
}
}
buffer.append(")");
if (!(tag instanceof PsiInlineDocTag)) {
buffer.append(" ");
}
else {
final int currentOffset = editor.getCaretModel().getOffset();
if (chars.charAt(currentOffset) == '}') {
afterSharp++;
}
else {
buffer.append("} ");
}
}
String insertString = buffer.toString();
EditorModificationUtil.insertStringAtCaret(editor, insertString);
editor.getCaretModel().moveToOffset(afterSharp + buffer.length());
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
shortenReferences(project, editor, context, afterParenth);
}