forked from casteng/i-pascal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[+] synchronize signature intention action
- Loading branch information
Showing
7 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
plugin/resources/intentionDescriptions/SynchronizeSignature/after.java.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
TTest = class | ||
procedure Test(Arg1: Integer; NewArg: string); | ||
end; | ||
|
||
procedure TTest.Test(Arg1: Integer<spot>; NewArg: string</spot>); | ||
begin | ||
end; |
7 changes: 7 additions & 0 deletions
7
plugin/resources/intentionDescriptions/SynchronizeSignature/before.java.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
TTest = class | ||
procedure Test(Arg1: Integer; NewArg: string); | ||
end; | ||
|
||
procedure TTest.Test(Arg1: Integer); | ||
begin | ||
end; |
5 changes: 5 additions & 0 deletions
5
plugin/resources/intentionDescriptions/SynchronizeSignature/description.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<html> | ||
<body> | ||
<p>This intention will synchronize routine or method signature with other (interface or implementation) part</p> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package com.siberika.idea.pascal.ide.intention; | ||
|
||
import com.intellij.codeInsight.intention.BaseElementAtCaretIntentionAction; | ||
import com.intellij.openapi.editor.Editor; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.util.PsiTreeUtil; | ||
import com.intellij.util.IncorrectOperationException; | ||
import com.siberika.idea.pascal.PascalBundle; | ||
import com.siberika.idea.pascal.ide.actions.SectionToggle; | ||
import com.siberika.idea.pascal.lang.psi.PasClassQualifiedIdent; | ||
import com.siberika.idea.pascal.lang.psi.PasConstrainedTypeParam; | ||
import com.siberika.idea.pascal.lang.psi.PasFormalParameterSection; | ||
import com.siberika.idea.pascal.lang.psi.PasNamedIdent; | ||
import com.siberika.idea.pascal.lang.psi.PasProcBodyBlock; | ||
import com.siberika.idea.pascal.lang.psi.PasRoutineImplDecl; | ||
import com.siberika.idea.pascal.lang.psi.PascalExportedRoutine; | ||
import com.siberika.idea.pascal.lang.psi.PascalRoutine; | ||
import com.siberika.idea.pascal.util.PsiUtil; | ||
import org.jetbrains.annotations.Nls; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
class SynchronizeSignature extends BaseElementAtCaretIntentionAction { | ||
|
||
@NotNull | ||
@Override | ||
public String getText() { | ||
return PascalBundle.message("action.fix.signature.synchronize"); | ||
} | ||
|
||
@Nls(capitalization = Nls.Capitalization.Sentence) | ||
@NotNull | ||
@Override | ||
public String getFamilyName() { | ||
return PascalBundle.message("action.fix.signature.synchronize.family"); | ||
} | ||
|
||
@Override | ||
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) { | ||
PascalRoutine routine = getRoutineHeader(editor, element); | ||
PascalRoutine target = getTargetRoutine(editor, element); | ||
return (routine != null) && (target != null) && !getSignature(routine).equals(getSignature(target)); | ||
} | ||
|
||
@Override | ||
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException { | ||
PascalRoutine routine = getRoutineHeader(editor, element); | ||
PascalRoutine target = getTargetRoutine(editor, element); | ||
if ((null == routine) || (null == target)) { | ||
return; | ||
} | ||
PasFormalParameterSection params = routine.getFormalParameterSection(); | ||
PasFormalParameterSection targetParams = target.getFormalParameterSection(); | ||
if (null == params) { | ||
if (targetParams != null) { | ||
targetParams.delete(); | ||
} | ||
} else { | ||
if (targetParams != null) { | ||
targetParams.replace(params); | ||
} else { | ||
PsiElement anchor = null; | ||
for (PsiElement child : target.getChildren()) { | ||
if (PsiUtil.isInstanceOfAny(child, PasNamedIdent.class, PasClassQualifiedIdent.class)) { | ||
anchor = child; | ||
} | ||
if (child instanceof PasConstrainedTypeParam) { | ||
anchor = child.getNextSibling(); // closing > | ||
} | ||
} | ||
if (anchor != null) { | ||
target.addAfter(params, anchor); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private String getSignature(PascalRoutine routine) { | ||
PasFormalParameterSection params = routine != null ? routine.getFormalParameterSection() : null; | ||
return params != null ? params.getText() : ""; | ||
} | ||
|
||
private PascalRoutine getTargetRoutine(Editor editor, PsiElement element) { | ||
PascalRoutine routine = getRoutineHeader(editor, element); | ||
PsiElement target = SectionToggle.getRoutineTarget(routine); | ||
return target instanceof PascalRoutine ? (PascalRoutine) target : null; | ||
} | ||
|
||
private PascalRoutine getRoutineHeader(Editor editor, PsiElement element) { | ||
PascalRoutine routine = PsiTreeUtil.getParentOfType(element, PascalRoutine.class); | ||
if (routine instanceof PascalExportedRoutine) { | ||
return routine; | ||
} else if (routine instanceof PasRoutineImplDecl) { | ||
PasProcBodyBlock block = ((PasRoutineImplDecl) routine).getProcBodyBlock(); | ||
Integer blockOffs = block != null ? block.getTextOffset() : null; | ||
return (null == blockOffs) || (editor.getCaretModel().getOffset() < blockOffs) ? routine : null; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters