update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / codeInsight / template / macro / SuggestIndexNameMacro.java
blob4cdae763a1542e7b756dc0bfdd57e85f7a7560ea
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.codeInsight.template.macro;
18 import com.intellij.codeInsight.CodeInsightBundle;
19 import com.intellij.codeInsight.lookup.LookupElement;
20 import com.intellij.codeInsight.template.*;
21 import com.intellij.openapi.project.Project;
22 import com.intellij.psi.*;
23 import com.intellij.psi.util.PsiTreeUtil;
24 import org.jetbrains.annotations.NotNull;
26 public class SuggestIndexNameMacro implements Macro{
27 public String getName() {
28 return "suggestIndexName";
31 public String getDescription() {
32 return CodeInsightBundle.message("macro.suggest.index.name");
35 public String getDefaultValue() {
36 return "a";
39 public Result calculateResult(@NotNull Expression[] params, final ExpressionContext context) {
40 if (params.length != 0) return null;
42 final Project project = context.getProject();
43 final int offset = context.getStartOffset();
45 PsiDocumentManager.getInstance(project).commitAllDocuments();
47 PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(context.getEditor().getDocument());
48 PsiElement place = file.findElementAt(offset);
49 PsiVariable[] vars = MacroUtil.getVariablesVisibleAt(place, "");
50 ChooseLetterLoop:
51 for(char letter = 'i'; letter <= 'z'; letter++){
52 for (PsiVariable var : vars) {
53 PsiIdentifier identifier = var.getNameIdentifier();
54 if (place.equals(identifier)) continue;
55 if (var instanceof PsiLocalVariable) {
56 PsiElement parent = var.getParent();
57 if (parent instanceof PsiDeclarationStatement) {
58 if (PsiTreeUtil.isAncestor(parent, place, false) &&
59 var.getTextRange().getStartOffset() > place.getTextRange().getStartOffset()) {
60 continue;
64 String name = identifier.getText();
65 if (name.length() == 1 && name.charAt(0) == letter) {
66 continue ChooseLetterLoop;
69 return new TextResult("" + letter);
72 return null;
75 public Result calculateQuickResult(@NotNull Expression[] params, ExpressionContext context) {
76 return null;
79 public LookupElement[] calculateLookupItems(@NotNull Expression[] params, ExpressionContext context) {
80 return null;