diff --git a/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/ArrayVariableMacro.java b/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/ArrayVariableMacro.java index b060d8001..24210b957 100644 --- a/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/ArrayVariableMacro.java +++ b/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/ArrayVariableMacro.java @@ -13,9 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package consulo.csharp.impl.ide.liveTemplates.macro; +import consulo.annotation.access.RequiredReadAction; import consulo.annotation.component.ExtensionImpl; import consulo.csharp.lang.impl.psi.source.resolve.type.CSharpArrayTypeRef; import consulo.dotnet.psi.DotNetVariable; @@ -23,52 +23,47 @@ import consulo.language.editor.template.Expression; import consulo.language.editor.template.ExpressionContext; import consulo.language.psi.PsiElement; - +import consulo.localize.LocalizeValue; import org.jspecify.annotations.Nullable; + import java.util.ArrayList; import java.util.List; /** * @author VISTALL - * @since 30.12.14 + * @since 2014-12-30 */ @ExtensionImpl -public class ArrayVariableMacro extends VariableTypeMacroBase -{ - @Nullable - @Override - protected PsiElement[] getVariables(Expression[] params, ExpressionContext context) - { - final PsiElement psiElementAtStartOffset = context.getPsiElementAtStartOffset(); - if(psiElementAtStartOffset == null) - { - return PsiElement.EMPTY_ARRAY; - } +public class ArrayVariableMacro extends VariableTypeMacroBase { + @Nullable + @Override + @RequiredReadAction + protected PsiElement[] getVariables(Expression[] params, ExpressionContext context) { + PsiElement psiElementAtStartOffset = context.getPsiElementAtStartOffset(); + if (psiElementAtStartOffset == null) { + return PsiElement.EMPTY_ARRAY; + } - List variables = CSharpLiveTemplateMacroUtil.resolveAllVariables(context.getPsiElementAtStartOffset()); + List variables = CSharpLiveTemplateMacroUtil.resolveAllVariables(context.getPsiElementAtStartOffset()); - List list = new ArrayList<>(); - for(DotNetVariable variable : variables) - { - DotNetTypeRef typeRefOfVariable = variable.toTypeRef(true); + List list = new ArrayList<>(); + for (DotNetVariable variable : variables) { + DotNetTypeRef typeRefOfVariable = variable.toTypeRef(true); - if(typeRefOfVariable instanceof CSharpArrayTypeRef && ((CSharpArrayTypeRef) typeRefOfVariable).getDimensions() == 0) - { - list.add(variable); - } - } - return list.toArray(new PsiElement[list.size()]); - } + if (typeRefOfVariable instanceof CSharpArrayTypeRef arrayTypeRef && arrayTypeRef.getDimensions() == 0) { + list.add(variable); + } + } + return list.toArray(new PsiElement[list.size()]); + } - @Override - public String getName() - { - return "csharpArrayVariable"; - } + @Override + public String getName() { + return "csharpArrayVariable"; + } - @Override - public String getPresentableName() - { - return "array variable"; - } + @Override + public LocalizeValue getPresentableName() { + return LocalizeValue.localizeTODO("array variable"); + } } diff --git a/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/CSharpLiveTemplateMacroUtil.java b/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/CSharpLiveTemplateMacroUtil.java index a73845ef5..31b38d7af 100644 --- a/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/CSharpLiveTemplateMacroUtil.java +++ b/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/CSharpLiveTemplateMacroUtil.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package consulo.csharp.impl.ide.liveTemplates.macro; import java.util.LinkedList; @@ -35,38 +34,39 @@ /** * @author VISTALL - * @since 29.12.14 + * @since 2014-12-29 */ -public class CSharpLiveTemplateMacroUtil -{ - @RequiredReadAction - public static List resolveAllVariables(PsiElement scope) - { - Couple resolveLayers = CSharpReferenceExpressionImplUtil.getResolveLayers(scope, false); +public class CSharpLiveTemplateMacroUtil { + @RequiredReadAction + public static List resolveAllVariables(PsiElement scope) { + Couple resolveLayers = CSharpReferenceExpressionImplUtil.getResolveLayers(scope, false); - AsPsiElementProcessor psiElementProcessor = new AsPsiElementProcessor(); - StubScopeProcessor processor = new SimpleNamedScopeProcessor(psiElementProcessor, true, ExecuteTarget.LOCAL_VARIABLE_OR_PARAMETER_OR_LOCAL_METHOD); - CSharpResolveUtil.treeWalkUp(processor, scope, scope, resolveLayers.getFirst()); + AsPsiElementProcessor psiElementProcessor = new AsPsiElementProcessor(); + StubScopeProcessor processor = + new SimpleNamedScopeProcessor(psiElementProcessor, true, ExecuteTarget.LOCAL_VARIABLE_OR_PARAMETER_OR_LOCAL_METHOD); + CSharpResolveUtil.treeWalkUp(processor, scope, scope, resolveLayers.getFirst()); - CSharpResolveOptions options = CSharpResolveOptions.build(); - options.element(scope); + CSharpResolveOptions options = CSharpResolveOptions.build(); + options.element(scope); - processor = new CompletionResolveScopeProcessor(options, psiElementProcessor, new ExecuteTarget[]{ - ExecuteTarget.FIELD, - ExecuteTarget.PROPERTY, - ExecuteTarget.EVENT - }); + processor = new CompletionResolveScopeProcessor( + options, + psiElementProcessor, + new ExecuteTarget[]{ + ExecuteTarget.FIELD, + ExecuteTarget.PROPERTY, + ExecuteTarget.EVENT + } + ); - CSharpResolveUtil.walkChildren(processor, resolveLayers.getSecond(), true, false, ResolveState.initial()); + CSharpResolveUtil.walkChildren(processor, resolveLayers.getSecond(), true, false, ResolveState.initial()); - List list = new LinkedList(); - for(PsiElement element : psiElementProcessor.getElements()) - { - if(element instanceof DotNetVariable) - { - list.add((DotNetVariable) element); - } - } - return list; - } + List list = new LinkedList<>(); + for (PsiElement element : psiElementProcessor.getElements()) { + if (element instanceof DotNetVariable variable) { + list.add(variable); + } + } + return list; + } } diff --git a/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/ForeachComponentTypeMacro.java b/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/ForeachComponentTypeMacro.java index b58a7289a..45c045581 100644 --- a/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/ForeachComponentTypeMacro.java +++ b/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/ForeachComponentTypeMacro.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package consulo.csharp.impl.ide.liveTemplates.macro; import consulo.annotation.access.RequiredReadAction; @@ -36,6 +35,7 @@ import consulo.language.editor.template.macro.Macro; import consulo.language.psi.PsiElement; import consulo.language.psi.util.PsiTreeUtil; +import consulo.localize.LocalizeValue; import consulo.util.collection.SmartList; import org.jspecify.annotations.Nullable; @@ -44,101 +44,88 @@ /** * @author VISTALL - * @since 11.06.14 + * @since 2014-06-11 */ @ExtensionImpl -public class ForeachComponentTypeMacro extends Macro -{ - @Override - public String getName() - { - return "csharpForeachComponentType"; - } - - @Override - public String getPresentableName() - { - return "csharpForeachComponentType(VARIABLE)"; - } - - @Override - public String getDefaultValue() - { - return "var"; - } - - @Nullable - @Override - @RequiredReadAction - public Result calculateQuickResult(Expression[] params, ExpressionContext context) - { - return calculateResult(params, context); - } - - @Nullable - @Override - @RequiredReadAction - public LookupElement[] calculateLookupItems(Expression[] params, ExpressionContext context) - { - Result result = calculateResult(params, context); - if(result == null) - { - return LookupElement.EMPTY_ARRAY; - } - List list = new SmartList<>(); - - boolean useVarForExtractLocalVariable = CSharpCodeGenerationSettings.getInstance(context.getProject()).USE_VAR_FOR_EXTRACT_LOCAL_VARIABLE; - boolean varSupported = CSharpModuleUtil.findLanguageVersion(context.getPsiElementAtStartOffset()).isAtLeast(CSharpLanguageVersion._2_0); - - boolean canUseVar = varSupported && useVarForExtractLocalVariable; - - if(canUseVar) - { - list.add(LookupElementBuilder.create("var").bold()); - } - else - { - list.add(LookupElementBuilder.create(result.toString())); - - if(varSupported) - { - list.add(LookupElementBuilder.create("var").bold()); - } - } - return list.toArray(new LookupElement[list.size()]); - } - - @Nullable - @Override - @RequiredReadAction - public Result calculateResult(Expression[] params, ExpressionContext context) - { - if(params.length != 1) - { - return null; - } - Result result = params[0].calculateResult(context); - if(result == null) - { - return null; - } - String text = result.toString(); - - PsiElement place = context.getPsiElementAtStartOffset(); - CSharpFragmentFileImpl expressionFragment = CSharpFragmentFactory.createExpressionFragment(context.getProject(), text, place); - - DotNetExpression expression = PsiTreeUtil.getChildOfType(expressionFragment, DotNetExpression.class); - - if(expression == null) - { - return null; - } - - DotNetTypeRef typeRef = CSharpResolveUtil.resolveIterableType(expression.toTypeRef(false)); - if(typeRef == DotNetTypeRef.ERROR_TYPE) - { - return null; - } - return new TextResult(CSharpTypeRefPresentationUtil.buildShortText(typeRef)); - } +public class ForeachComponentTypeMacro extends Macro { + @Override + public String getName() { + return "csharpForeachComponentType"; + } + + @Override + public LocalizeValue getPresentableName() { + return LocalizeValue.of("csharpForeachComponentType(VARIABLE)"); + } + + @Override + public String getDefaultValue() { + return "var"; + } + + @Nullable + @Override + @RequiredReadAction + public Result calculateQuickResult(Expression[] params, ExpressionContext context) { + return calculateResult(params, context); + } + + @Nullable + @Override + @RequiredReadAction + public LookupElement[] calculateLookupItems(Expression[] params, ExpressionContext context) { + Result result = calculateResult(params, context); + if (result == null) { + return LookupElement.EMPTY_ARRAY; + } + List list = new SmartList<>(); + + boolean useVarForExtractLocalVariable = + CSharpCodeGenerationSettings.getInstance(context.getProject()).USE_VAR_FOR_EXTRACT_LOCAL_VARIABLE; + boolean varSupported = + CSharpModuleUtil.findLanguageVersion(context.getPsiElementAtStartOffset()).isAtLeast(CSharpLanguageVersion._2_0); + + boolean canUseVar = varSupported && useVarForExtractLocalVariable; + + if (canUseVar) { + list.add(LookupElementBuilder.create("var").bold()); + } + else { + list.add(LookupElementBuilder.create(result.toString())); + + if (varSupported) { + list.add(LookupElementBuilder.create("var").bold()); + } + } + return list.toArray(new LookupElement[list.size()]); + } + + @Nullable + @Override + @RequiredReadAction + public Result calculateResult(Expression[] params, ExpressionContext context) { + if (params.length != 1) { + return null; + } + Result result = params[0].calculateResult(context); + if (result == null) { + return null; + } + String text = result.toString(); + + PsiElement place = context.getPsiElementAtStartOffset(); + CSharpFragmentFileImpl expressionFragment = CSharpFragmentFactory.createExpressionFragment(context.getProject(), text, place); + + DotNetExpression expression = PsiTreeUtil.getChildOfType(expressionFragment, DotNetExpression.class); + + if (expression == null) { + return null; + } + + DotNetTypeRef typeRef = CSharpResolveUtil.resolveIterableType(expression.toTypeRef(false)); + if (typeRef == DotNetTypeRef.ERROR_TYPE) { + return null; + } + return new TextResult(CSharpTypeRefPresentationUtil.buildShortText(typeRef)); + } } diff --git a/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/ForeachVariableMacro.java b/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/ForeachVariableMacro.java index e7a83acb1..12167fc26 100644 --- a/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/ForeachVariableMacro.java +++ b/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/ForeachVariableMacro.java @@ -13,9 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package consulo.csharp.impl.ide.liveTemplates.macro; +import consulo.annotation.access.RequiredReadAction; import consulo.annotation.component.ExtensionImpl; import consulo.csharp.lang.impl.psi.DotNetTypes2; import consulo.csharp.lang.impl.psi.source.CSharpTypeDeclarationImplUtil; @@ -24,6 +24,7 @@ import consulo.language.editor.template.Expression; import consulo.language.editor.template.ExpressionContext; import consulo.language.psi.PsiElement; +import consulo.localize.LocalizeValue; import consulo.util.collection.SmartList; import org.jspecify.annotations.Nullable; @@ -31,50 +32,44 @@ /** * @author VISTALL - * @since 11.06.14 + * @since 2014-06-11 */ @ExtensionImpl -public class ForeachVariableMacro extends VariableTypeMacroBase -{ - private static final String[] ourAcceptableTypes = { - DotNetTypes2.System.Collections.IEnumerable, - DotNetTypes2.System.Collections.Generic.IEnumerable$1 - }; +public class ForeachVariableMacro extends VariableTypeMacroBase { + private static final String[] ourAcceptableTypes = { + DotNetTypes2.System.Collections.IEnumerable, + DotNetTypes2.System.Collections.Generic.IEnumerable$1 + }; - @Nullable - @Override - protected PsiElement[] getVariables(Expression[] params, ExpressionContext context) - { - final PsiElement psiElementAtStartOffset = context.getPsiElementAtStartOffset(); - if(psiElementAtStartOffset == null) - { - return PsiElement.EMPTY_ARRAY; - } + @Nullable + @Override + @RequiredReadAction + protected PsiElement[] getVariables(Expression[] params, ExpressionContext context) { + PsiElement psiElementAtStartOffset = context.getPsiElementAtStartOffset(); + if (psiElementAtStartOffset == null) { + return PsiElement.EMPTY_ARRAY; + } - List variables = CSharpLiveTemplateMacroUtil.resolveAllVariables(context.getPsiElementAtStartOffset()); + List variables = CSharpLiveTemplateMacroUtil.resolveAllVariables(context.getPsiElementAtStartOffset()); - List list = new SmartList(); - for(DotNetVariable variable : variables) - { - DotNetTypeRef typeRefOfVariable = variable.toTypeRef(true); + List list = new SmartList<>(); + for (DotNetVariable variable : variables) { + DotNetTypeRef typeRefOfVariable = variable.toTypeRef(true); - if(CSharpTypeDeclarationImplUtil.isInheritOrSelf(typeRefOfVariable, psiElementAtStartOffset, ourAcceptableTypes)) - { - list.add(variable); - } - } - return list.toArray(new PsiElement[list.size()]); - } + if (CSharpTypeDeclarationImplUtil.isInheritOrSelf(typeRefOfVariable, psiElementAtStartOffset, ourAcceptableTypes)) { + list.add(variable); + } + } + return list.toArray(new PsiElement[list.size()]); + } - @Override - public String getName() - { - return "csharpForeachVariable"; - } + @Override + public String getName() { + return "csharpForeachVariable"; + } - @Override - public String getPresentableName() - { - return "foreach variable"; - } + @Override + public LocalizeValue getPresentableName() { + return LocalizeValue.localizeTODO("foreach variable"); + } } diff --git a/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/IListVariableMacro.java b/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/IListVariableMacro.java index b26992037..e2a74b93f 100644 --- a/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/IListVariableMacro.java +++ b/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/IListVariableMacro.java @@ -13,9 +13,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package consulo.csharp.impl.ide.liveTemplates.macro; +import consulo.annotation.access.RequiredReadAction; import consulo.annotation.component.ExtensionImpl; import consulo.csharp.lang.impl.psi.DotNetTypes2; import consulo.csharp.lang.impl.psi.source.CSharpTypeDeclarationImplUtil; @@ -25,52 +25,51 @@ import consulo.language.editor.template.ExpressionContext; import consulo.language.psi.PsiElement; +import consulo.localize.LocalizeValue; import org.jspecify.annotations.Nullable; + import java.util.ArrayList; import java.util.List; /** * @author VISTALL - * @since 30.12.14 + * @since 2014-12-30 */ @ExtensionImpl -public class IListVariableMacro extends VariableTypeMacroBase -{ - @Nullable - @Override - protected PsiElement[] getVariables(Expression[] params, ExpressionContext context) - { - final PsiElement psiElementAtStartOffset = context.getPsiElementAtStartOffset(); - if(psiElementAtStartOffset == null) - { - return PsiElement.EMPTY_ARRAY; - } +public class IListVariableMacro extends VariableTypeMacroBase { + @Nullable + @Override + @RequiredReadAction + protected PsiElement[] getVariables(Expression[] params, ExpressionContext context) { + PsiElement psiElementAtStartOffset = context.getPsiElementAtStartOffset(); + if (psiElementAtStartOffset == null) { + return PsiElement.EMPTY_ARRAY; + } - List variables = CSharpLiveTemplateMacroUtil.resolveAllVariables(context.getPsiElementAtStartOffset()); + List variables = CSharpLiveTemplateMacroUtil.resolveAllVariables(context.getPsiElementAtStartOffset()); - List list = new ArrayList<>(); - for(DotNetVariable variable : variables) - { - DotNetTypeRef typeRefOfVariable = variable.toTypeRef(true); + List list = new ArrayList<>(); + for (DotNetVariable variable : variables) { + DotNetTypeRef typeRefOfVariable = variable.toTypeRef(true); - if(CSharpTypeDeclarationImplUtil.isInheritOrSelf(typeRefOfVariable, psiElementAtStartOffset, DotNetTypes2.System.Collections.Generic - .IList$1)) - { - list.add(variable); - } - } - return list.toArray(new PsiElement[list.size()]); - } + if (CSharpTypeDeclarationImplUtil.isInheritOrSelf( + typeRefOfVariable, + psiElementAtStartOffset, + DotNetTypes2.System.Collections.Generic.IList$1 + )) { + list.add(variable); + } + } + return list.toArray(new PsiElement[list.size()]); + } - @Override - public String getName() - { - return "csharpIListVariable"; - } + @Override + public String getName() { + return "csharpIListVariable"; + } - @Override - public String getPresentableName() - { - return "list variable"; - } + @Override + public LocalizeValue getPresentableName() { + return LocalizeValue.localizeTODO("list variable"); + } } diff --git a/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/SuggestIndexVariableNameMacro.java b/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/SuggestIndexVariableNameMacro.java index 5fb27952c..a80c43489 100644 --- a/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/SuggestIndexVariableNameMacro.java +++ b/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/SuggestIndexVariableNameMacro.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package consulo.csharp.impl.ide.liveTemplates.macro; import consulo.annotation.component.ExtensionImpl; @@ -27,6 +26,7 @@ import consulo.language.psi.PsiDocumentManager; import consulo.language.psi.PsiElement; import consulo.language.psi.PsiFile; +import consulo.localize.LocalizeValue; import consulo.project.Project; import consulo.ui.annotation.RequiredUIAccess; import org.jspecify.annotations.Nullable; @@ -35,63 +35,54 @@ /** * @author VISTALL - * @since 29.12.14 + * @since 2014-12-29 */ @ExtensionImpl -public class SuggestIndexVariableNameMacro extends Macro -{ - @Override - public String getDefaultValue() - { - return "i"; - } +public class SuggestIndexVariableNameMacro extends Macro { + @Override + public String getDefaultValue() { + return "i"; + } - @Override - public String getName() - { - return "csharpSuggestIndexName"; - } + @Override + public String getName() { + return "csharpSuggestIndexName"; + } - @Override - public String getPresentableName() - { - return "csharpSuggestIndexName()"; - } + @Override + public LocalizeValue getPresentableName() { + return LocalizeValue.of("csharpSuggestIndexName()"); + } - @Nullable - @Override - @RequiredUIAccess - public Result calculateResult(Expression[] params, ExpressionContext context) - { - final Project project = context.getProject(); - final int offset = context.getStartOffset(); + @Nullable + @Override + @RequiredUIAccess + public Result calculateResult(Expression[] params, ExpressionContext context) { + Project project = context.getProject(); + int offset = context.getStartOffset(); - PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(context.getEditor().getDocument()); - PsiElement place = file.findElementAt(offset); + PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(context.getEditor().getDocument()); + PsiElement place = file.findElementAt(offset); - List dotNetVariables = CSharpLiveTemplateMacroUtil.resolveAllVariables(place); + List dotNetVariables = CSharpLiveTemplateMacroUtil.resolveAllVariables(place); - DotNetVariable variable = CSharpLineMarkerUtil.getNameIdentifierAs(place, DotNetVariable.class); + DotNetVariable variable = CSharpLineMarkerUtil.getNameIdentifierAs(place, DotNetVariable.class); - ChooseLetterLoop: - for(char letter = 'i'; letter <= 'z'; letter++) - { - for(DotNetVariable dotNetVariable : dotNetVariables) - { - // skip self - if(dotNetVariable == variable) - { - continue; - } + ChooseLetterLoop: + for (char letter = 'i'; letter <= 'z'; letter++) { + for (DotNetVariable dotNetVariable : dotNetVariables) { + // skip self + if (dotNetVariable == variable) { + continue; + } - String name = dotNetVariable.getName(); - if(name != null && name.length() == 1 && name.charAt(0) == letter) - { - continue ChooseLetterLoop; - } - } - return new TextResult(String.valueOf(letter)); - } - return null; - } + String name = dotNetVariable.getName(); + if (name != null && name.length() == 1 && name.charAt(0) == letter) { + continue ChooseLetterLoop; + } + } + return new TextResult(String.valueOf(letter)); + } + return null; + } } diff --git a/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/SuggestVariableNameMacro.java b/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/SuggestVariableNameMacro.java index 6ea440951..2907d6a14 100644 --- a/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/SuggestVariableNameMacro.java +++ b/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/SuggestVariableNameMacro.java @@ -13,11 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package consulo.csharp.impl.ide.liveTemplates.macro; import consulo.annotation.component.ExtensionImpl; -import consulo.application.ApplicationManager; +import consulo.application.Application; import consulo.csharp.impl.ide.lineMarkerProvider.CSharpLineMarkerUtil; import consulo.csharp.impl.ide.refactoring.util.CSharpNameSuggesterUtil; import consulo.csharp.lang.psi.CSharpTokens; @@ -31,12 +30,14 @@ import consulo.language.editor.template.macro.Macro; import consulo.language.psi.*; import consulo.language.util.IncorrectOperationException; +import consulo.localize.LocalizeValue; import consulo.logging.Logger; import consulo.project.Project; import consulo.ui.annotation.RequiredUIAccess; import consulo.util.collection.ContainerUtil; import org.jspecify.annotations.Nullable; + import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -44,109 +45,89 @@ /** * @author VISTALL - * @since 11.06.14 + * @since 2014-06-11 */ @ExtensionImpl -public class SuggestVariableNameMacro extends Macro -{ - private static final Logger LOGGER = Logger.getInstance(SuggestVariableNameMacro.class); +public class SuggestVariableNameMacro extends Macro { + private static final Logger LOGGER = Logger.getInstance(SuggestVariableNameMacro.class); - @Override - public String getName() - { - return "csharpSuggestVariableName"; - } + @Override + public String getName() { + return "csharpSuggestVariableName"; + } - @Override - public String getPresentableName() - { - return "suggestVariableName variable"; - } + @Override + public LocalizeValue getPresentableName() { + return LocalizeValue.localizeTODO("suggestVariableName variable"); + } - @Override - public String getDefaultValue() - { - return "it"; - } + @Override + public String getDefaultValue() { + return "it"; + } - @Nullable - @Override - @RequiredUIAccess - public Result calculateQuickResult(Expression[] params, ExpressionContext context) - { - return calculateResult(params, context); - } + @Nullable + @Override + @RequiredUIAccess + public Result calculateQuickResult(Expression[] params, ExpressionContext context) { + return calculateResult(params, context); + } - @Nullable - @Override - @RequiredUIAccess - public LookupElement[] calculateLookupItems(Expression[] params, ExpressionContext context) - { - Collection suggestedVariableNames = getSuggestedVariableNames(context); + @Nullable + @Override + @RequiredUIAccess + public LookupElement[] calculateLookupItems(Expression[] params, ExpressionContext context) { + Collection suggestedVariableNames = getSuggestedVariableNames(context); - List list = new ArrayList(suggestedVariableNames.size()); - for(String temp : suggestedVariableNames) - { - list.add(LookupElementBuilder.create(temp)); - } - return list.toArray(new LookupElement[list.size()]); - } + List list = new ArrayList<>(suggestedVariableNames.size()); + for (String temp : suggestedVariableNames) { + list.add(LookupElementBuilder.create(temp)); + } + return list.toArray(new LookupElement[list.size()]); + } - @Nullable - @Override - @RequiredUIAccess - public Result calculateResult(Expression[] params, ExpressionContext context) - { - Collection suggestedVariableNames = getSuggestedVariableNames(context); - return new TextResult(ContainerUtil.getFirstItem(suggestedVariableNames, "it")); - } + @Nullable + @Override + @RequiredUIAccess + public Result calculateResult(Expression[] params, ExpressionContext context) { + Collection suggestedVariableNames = getSuggestedVariableNames(context); + return new TextResult(ContainerUtil.getFirstItem(suggestedVariableNames, "it")); + } - @RequiredUIAccess - private Collection getSuggestedVariableNames(ExpressionContext context) - { - final Project project = context.getProject(); - final int offset = context.getStartOffset(); + @RequiredUIAccess + private Collection getSuggestedVariableNames(ExpressionContext context) { + Project project = context.getProject(); + int offset = context.getStartOffset(); - PsiDocumentManager.getInstance(project).commitAllDocuments(); + PsiDocumentManager.getInstance(project).commitAllDocuments(); - PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(context.getEditor().getDocument()); - assert file != null; - PsiElement element = file.findElementAt(offset); + PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(context.getEditor().getDocument()); + assert file != null; + PsiElement element = file.findElementAt(offset); - assert element != null; - if(PsiUtilCore.getElementType(element) == CSharpTokens.IDENTIFIER) - { - DotNetVariable variable = CSharpLineMarkerUtil.getNameIdentifierAs(element, DotNetVariable.class); - if(variable != null) - { - return CSharpNameSuggesterUtil.getSuggestedVariableNames(variable); - } - } - else - { - final PsiFile fileCopy = (PsiFile) file.copy(); - ApplicationManager.getApplication().runWriteAction(new Runnable() - { - @Override - public void run() - { - try - { - ReparseRangeUtil.reparseRange(fileCopy, offset, offset, "xxx"); - } - catch(IncorrectOperationException e) - { - LOGGER.error(e); - } + assert element != null; + if (PsiUtilCore.getElementType(element) == CSharpTokens.IDENTIFIER) { + DotNetVariable variable = CSharpLineMarkerUtil.getNameIdentifierAs(element, DotNetVariable.class); + if (variable != null) { + return CSharpNameSuggesterUtil.getSuggestedVariableNames(variable); + } + } + else { + PsiFile fileCopy = (PsiFile) file.copy(); + Application.get().runWriteAction(() -> { + try { + ReparseRangeUtil.reparseRange(fileCopy, offset, offset, "xxx"); + } + catch (IncorrectOperationException e) { + LOGGER.error(e); } }); - PsiElement identifierCopy = fileCopy.findElementAt(offset); - DotNetVariable variable = CSharpLineMarkerUtil.getNameIdentifierAs(identifierCopy, DotNetVariable.class); - if(variable != null) - { - return CSharpNameSuggesterUtil.getSuggestedVariableNames(variable); - } - } - return Collections.emptyList(); - } + PsiElement identifierCopy = fileCopy.findElementAt(offset); + DotNetVariable variable = CSharpLineMarkerUtil.getNameIdentifierAs(identifierCopy, DotNetVariable.class); + if (variable != null) { + return CSharpNameSuggesterUtil.getSuggestedVariableNames(variable); + } + } + return Collections.emptyList(); + } } diff --git a/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/TypeMacro.java b/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/TypeMacro.java index 0d174e09a..804d44813 100644 --- a/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/TypeMacro.java +++ b/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/TypeMacro.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package consulo.csharp.impl.ide.liveTemplates.macro; import consulo.annotation.component.ExtensionImpl; @@ -22,31 +21,28 @@ import consulo.language.editor.template.Result; import consulo.language.editor.template.TextResult; import consulo.language.editor.template.macro.Macro; +import consulo.localize.LocalizeValue; import org.jspecify.annotations.Nullable; /** * @author VISTALL - * @since 07.03.2016 + * @since 2016-03.07 */ @ExtensionImpl -public class TypeMacro extends Macro -{ - @Override - public String getName() - { - return "csharpType"; - } +public class TypeMacro extends Macro { + @Override + public String getName() { + return "csharpType"; + } - @Override - public String getPresentableName() - { - return "type"; - } + @Override + public LocalizeValue getPresentableName() { + return LocalizeValue.localizeTODO("type"); + } - @Nullable - @Override - public Result calculateResult(Expression[] params, ExpressionContext context) - { - return new TextResult("TYPE"); - } + @Nullable + @Override + public Result calculateResult(Expression[] params, ExpressionContext context) { + return new TextResult("TYPE"); + } } diff --git a/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/VariableTypeMacroBase.java b/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/VariableTypeMacroBase.java index 6c77fc6bd..27ea4ce84 100644 --- a/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/VariableTypeMacroBase.java +++ b/csharp-impl/src/main/java/consulo/csharp/impl/ide/liveTemplates/macro/VariableTypeMacroBase.java @@ -30,57 +30,46 @@ import org.jspecify.annotations.Nullable; /** + * from java plugin + * * @author VISTALL * @author ven - *

- * from java plugin * @since 11.06.14 - *

- * base code by */ -public abstract class VariableTypeMacroBase extends Macro -{ - @Nullable - protected abstract PsiElement[] getVariables(Expression[] params, final ExpressionContext context); +public abstract class VariableTypeMacroBase extends Macro { + @Nullable + protected abstract PsiElement[] getVariables(Expression[] params, ExpressionContext context); - @Override - @RequiredReadAction - public LookupElement[] calculateLookupItems(Expression[] params, final ExpressionContext context) - { - final PsiElement[] vars = getVariables(params, context); - if(vars == null || vars.length < 2) - { - return null; - } - return CSharpLookupElementBuilder.buildToLookupElements(vars); - } + @Override + @RequiredReadAction + public LookupElement[] calculateLookupItems(Expression[] params, ExpressionContext context) { + PsiElement[] vars = getVariables(params, context); + if (vars == null || vars.length < 2) { + return null; + } + return CSharpLookupElementBuilder.buildToLookupElements(vars); + } - @Override - public Result calculateResult(Expression[] params, ExpressionContext context) - { - final PsiElement[] vars = getVariables(params, context); - if(vars == null || vars.length == 0) - { - return null; - } - return new PsiElementResult(vars[0]) - { - @Override - public String toString() - { - PsiElement element = getElement(); - if(element instanceof DotNetVariable) - { - return ((DotNetVariable) element).getName(); - } - return super.toString(); - } - }; - } + @Override + public Result calculateResult(Expression[] params, ExpressionContext context) { + final PsiElement[] vars = getVariables(params, context); + if (vars == null || vars.length == 0) { + return null; + } + return new PsiElementResult(vars[0]) { + @Override + @RequiredReadAction + public String toString() { + if (getElement() instanceof DotNetVariable variable) { + return variable.getName(); + } + return super.toString(); + } + }; + } - @Override - public String getDefaultValue() - { - return "a"; - } + @Override + public String getDefaultValue() { + return "a"; + } }