Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,62 +13,57 @@
* 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;
import consulo.dotnet.psi.resolve.DotNetTypeRef;
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<DotNetVariable> variables = CSharpLiveTemplateMacroUtil.resolveAllVariables(context.getPsiElementAtStartOffset());
List<DotNetVariable> variables = CSharpLiveTemplateMacroUtil.resolveAllVariables(context.getPsiElementAtStartOffset());

List<DotNetVariable> list = new ArrayList<>();
for(DotNetVariable variable : variables)
{
DotNetTypeRef typeRefOfVariable = variable.toTypeRef(true);
List<DotNetVariable> 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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,38 +34,39 @@

/**
* @author VISTALL
* @since 29.12.14
* @since 2014-12-29
*/
public class CSharpLiveTemplateMacroUtil
{
@RequiredReadAction
public static List<DotNetVariable> resolveAllVariables(PsiElement scope)
{
Couple<PsiElement> resolveLayers = CSharpReferenceExpressionImplUtil.getResolveLayers(scope, false);
public class CSharpLiveTemplateMacroUtil {
@RequiredReadAction
public static List<DotNetVariable> resolveAllVariables(PsiElement scope) {
Couple<PsiElement> 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<DotNetVariable> list = new LinkedList<DotNetVariable>();
for(PsiElement element : psiElementProcessor.getElements())
{
if(element instanceof DotNetVariable)
{
list.add((DotNetVariable) element);
}
}
return list;
}
List<DotNetVariable> list = new LinkedList<>();
for (PsiElement element : psiElementProcessor.getElements()) {
if (element instanceof DotNetVariable variable) {
list.add(variable);
}
}
return list;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<LookupElement> 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<LookupElement> 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));
}
}
Loading
Loading