Skip to content

Commit

Permalink
adding intents for variable/property conversion and iterate in variab…
Browse files Browse the repository at this point in the history
…le with iterator
  • Loading branch information
m0rkeulv committed Dec 10, 2023
1 parent 2f4faf1 commit 1a4efc7
Show file tree
Hide file tree
Showing 23 changed files with 634 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.intellij.codeInsight.hints.declarative.*;
import com.intellij.openapi.editor.Editor;
import com.intellij.plugins.haxe.lang.psi.*;
import com.intellij.plugins.haxe.model.HaxeMemberModel;
import com.intellij.plugins.haxe.model.HaxeMethodModel;
import com.intellij.plugins.haxe.model.type.*;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
Expand Down Expand Up @@ -47,9 +49,24 @@ private static void handleForEachHints(@NotNull PsiElement element,
HaxeIteratorkey iteratorKey = keyValueIterator.getIteratorkey();
HaxeIteratorValue iteratorValue = keyValueIterator.getIteratorValue();

ResultHolder keyValueIteratorType = HaxeTypeResolver.getPsiElementType(iterable, element, resolver);
ResultHolder keyType = extractKeyValueType(keyValueIteratorType, true);
ResultHolder valueType = extractKeyValueType(keyValueIteratorType, false);
var keyValueIteratorType = HaxeTypeResolver.getPsiElementType(iterable, element, resolver);
var iteratorType = keyValueIteratorType.getClassType();
if (iteratorType.isTypeDef()) {
iteratorType = iteratorType.fullyResolveTypeDefClass();
}
var iteratorTypeResolver = iteratorType.getGenericResolver();

HaxeMethodModel iteratorReturnType = (HaxeMethodModel)iteratorType.getHaxeClassModel().getMember("next", iteratorTypeResolver);
HaxeGenericResolver nextResolver = iteratorReturnType.getGenericResolver(null);
nextResolver.addAll(iteratorTypeResolver);

ResultHolder returnType = iteratorReturnType.getReturnType(nextResolver);
SpecificHaxeClassReference type = returnType.getClassType();
HaxeGenericResolver genericResolver = type.getGenericResolver();

ResultHolder keyType = type.getHaxeClassModel().getMember("key", null).getResultType(genericResolver);
ResultHolder valueType = type.getHaxeClassModel().getMember("value", null).getResultType(genericResolver);


createInlayHint(iteratorKey.getComponentName(), sink, keyType);
createInlayHint(iteratorValue.getComponentName(), sink, valueType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public AddReturnTypeTagIntention() {
@NotNull
@Override
public String getFamilyName() {
return HaxeBundle.message("quick.fixes.family");
return getText();
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public AddTypeTagToFieldIntention() {
@NotNull
@Override
public String getFamilyName() {
return HaxeBundle.message("quick.fixes.family");
return getText();
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.intellij.plugins.haxe.ide.intention;

import com.intellij.codeInsight.intention.impl.BaseIntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.plugins.haxe.HaxeBundle;
import com.intellij.plugins.haxe.HaxeLanguage;
import com.intellij.plugins.haxe.lang.psi.*;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;


public class ConvertPropertyToVariableIntention extends BaseIntentionAction {

private HaxeFieldDeclaration myField;

@Nls
@NotNull
@Override
public String getFamilyName() {
return getText();
}

@NotNull
@Override
public String getText() {
return HaxeBundle.message("haxe.quickfix.property.to.var");
}


@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (file.getLanguage() != HaxeLanguage.INSTANCE) return false;
attemptToFindField(editor, file);

if (myField == null) return false;
if (myField.getTypeTag() == null) return false;
if (myField.getPropertyDeclaration() == null) return false;

return true;
}


@Override
public void invoke(@NotNull final Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
myField.getPropertyDeclaration().delete();
}





private void attemptToFindField(Editor editor, PsiFile file) {
PsiElement place = file.findElementAt(editor.getCaretModel().getOffset());
if (place instanceof HaxeFieldDeclaration psiField) {
myField = psiField;
} else {
myField = PsiTreeUtil.getParentOfType(place, HaxeFieldDeclaration.class);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.intellij.plugins.haxe.ide.intention;

import com.intellij.plugins.haxe.HaxeBundle;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class ConvertVariableToPropertyDefaultIntention extends ConvertVariableToPropertyIntentionBase {

@Nls
@NotNull
@Override
public String getFamilyName() {
return HaxeBundle.message("haxe.quickfix.var.to.property");
}

@NotNull
@Override
public String getText() {
return HaxeBundle.message("haxe.quickfix.var.to.property.default");
}


@NotNull
protected String getPropertyElementString() {
return "var tmp (default, default) :Int;";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.intellij.plugins.haxe.ide.intention;

import com.intellij.plugins.haxe.HaxeBundle;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class ConvertVariableToPropertyGetSetIntention extends ConvertVariableToPropertyIntentionBase {

@Nls
@NotNull
@Override
public String getFamilyName() {
return HaxeBundle.message("haxe.quickfix.var.to.property");
}

@NotNull
@Override
public String getText() {
return HaxeBundle.message("haxe.quickfix.var.to.property.getset");
}


@NotNull
protected String getPropertyElementString() {
return "var tmp (get, set) :Int;";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.intellij.plugins.haxe.ide.intention;

import com.intellij.codeInsight.intention.impl.BaseIntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.plugins.haxe.HaxeLanguage;
import com.intellij.plugins.haxe.lang.psi.*;
import com.intellij.plugins.haxe.model.HaxeFieldModel;
import com.intellij.plugins.haxe.util.HaxeElementGenerator;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public abstract class ConvertVariableToPropertyIntentionBase extends BaseIntentionAction {

private HaxePsiField myField;


@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (file.getLanguage() != HaxeLanguage.INSTANCE) return false;
attemptToFindField(editor, file);

if (myField == null) return false;

if (myField.getTypeTag() == null) return false;
if (myField.getVarInit() != null) return false;
if (myField.getModel() instanceof HaxeFieldModel model) {
if (model.hasModifier(HaxePsiModifier.INLINE)) return false;
if (model.isProperty()) return false;
}else {
return false;
}

return true;
}


@Override
public void invoke(@NotNull final Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
HaxePsiField element = getTempProperty(project);
PsiElement copy = HaxeElementGenerator.createVarDeclaration(project, element.getText()).copy();
myField.replace(copy);

}

@NotNull
private HaxePsiField getTempProperty(@NotNull Project project) {
HaxePsiField element = (HaxePsiField)myField.copy();
HaxePropertyDeclaration declaration = generateTmpDeclaration(project);
HaxeComponentName name = element.getComponentName();
HaxeIdentifier identifier = name.getIdentifier();

name.addAfter(declaration, identifier);
return element;
}

private HaxePropertyDeclaration generateTmpDeclaration(@NotNull Project project) {
return (HaxePropertyDeclaration)HaxeElementGenerator.createVarDeclaration(project, getPropertyElementString())
.getPropertyDeclaration().copy();
}

@NotNull
protected abstract String getPropertyElementString();


private void attemptToFindField(Editor editor, PsiFile file) {
PsiElement place = file.findElementAt(editor.getCaretModel().getOffset());
HaxeLocalVarDeclarationList varDeclarationList = PsiTreeUtil.getParentOfType(place, HaxeLocalVarDeclarationList.class);
if (varDeclarationList != null) {
List<HaxeLocalVarDeclaration> list = varDeclarationList.getLocalVarDeclarationList();
if (!list.isEmpty()) myField = list.get(list.size() - 1);
}
else if (place instanceof HaxePsiField psiField) {
myField = psiField;
}
else {
myField = PsiTreeUtil.getParentOfType(place, HaxePsiField.class);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.intellij.plugins.haxe.ide.intention;

import com.intellij.plugins.haxe.HaxeBundle;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;

public class ConvertVariableToPropertyReadOnlyIntention extends ConvertVariableToPropertyIntentionBase {

@Nls
@NotNull
@Override
public String getFamilyName() {
return HaxeBundle.message("haxe.quickfix.var.to.property");
}

@NotNull
@Override
public String getText() {
return HaxeBundle.message("haxe.quickfix.var.to.property.read.only");
}


@NotNull
protected String getPropertyElementString() {
return "var tmp (get, never) :Int;";
}
}
Loading

0 comments on commit 1a4efc7

Please sign in to comment.