Skip to content

Commit

Permalink
Auto-indent next line after "," in phpdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
klesun committed Jun 16, 2019
1 parent 59872d7 commit e28b45e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
58 changes: 38 additions & 20 deletions src/org/klesun/deep_assoc_completion/entry/DeepEnterHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocToken;
import com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocTag;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -27,26 +28,43 @@ public Result preprocessEnter(@NotNull PsiFile psiFile, @NotNull Editor editor,
String ch = docWr.sub(pos - 1, pos);
String nextCh = docWr.sub(pos, pos + 1);

return opt(PsiTreeUtil.findElementOfClassAtOffset(psiFile, pos - 1, PhpDocTag.class, false))
.flt(tag -> tag.getTagValue().matches("\\s*=.*"))
.flt(tag -> ch.equals("["))
.fop(tag -> Tls.regex(
".*\\n(\\s*\\*\\s*).*",
docWr.sub(pos - 100, pos)
).fop(match -> match.gat(0))
.map(baseIndent -> {
String insertion = "\n" + baseIndent + " ";
docWr.doc.insertString(pos, insertion);
int newPos = pos + insertion.length();
caret.moveToOffset(newPos);
if (nextCh.equals("]")) {
docWr.doc.insertString(newPos, "\n" + baseIndent);
} else if (tag.getTagValue().matches(".*\\[\\s*")) {
docWr.doc.insertString(newPos, "\n" + baseIndent + "]");
}
return Result.Stop;
}))
.def(Result.Continue);
if (ch.equals("[")) {
return opt(PsiTreeUtil.findElementOfClassAtOffset(psiFile, pos - 1, PhpDocTag.class, false))
.flt(tag -> tag.getTagValue().matches("\\s*=.*"))
.fop(tag -> Tls.regex(
".*\\n(\\s*\\*\\s*).*",
docWr.sub(pos - 100, pos)
).fop(match -> match.gat(0))
.map(baseIndent -> {
String insertion = "\n" + baseIndent + " ";
docWr.doc.insertString(pos, insertion);
int newPos = pos + insertion.length();
caret.moveToOffset(newPos);
if (nextCh.equals("]")) {
docWr.doc.insertString(newPos, "\n" + baseIndent);
} else if (tag.getTagValue().matches(".*\\[\\s*")) {
docWr.doc.insertString(newPos, "\n" + baseIndent + "]");
}
return Result.Stop;
}))
.def(Result.Continue);
} else if (ch.equals(",")) {
return opt(PsiTreeUtil.findElementOfClassAtOffset(psiFile, pos - 1, PhpDocToken.class, false))
.fop(tag -> Tls.regex(
".*\\n(\\s*\\*\\s*).*",
docWr.sub(pos - 100, pos)
).fop(match -> match.gat(0))
.map(baseIndent -> {
String insertion = "\n" + baseIndent;
docWr.doc.insertString(pos, insertion);
int newPos = pos + insertion.length();
caret.moveToOffset(newPos);
return Result.Stop;
}))
.def(Result.Continue);
} else {
return Result.Continue;
}
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion tests/src/DeepTest/UsageResolverUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ public function provideStaticInDoc($param)
}

/**
* @param $data = []
* @param $data
* @return array = ['key1' => '', 'key2' => 0]
*/
private function normDocData($data)
Expand Down

0 comments on commit e28b45e

Please sign in to comment.