Files
openidea/src/editor/core/cm/docComment.test.ts
T

156 lines
6.4 KiB
TypeScript

import { afterEach, describe, expect, it } from "vitest";
import { EditorSelection, EditorState } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import { undo, history } from "@codemirror/commands";
import { java } from "@codemirror/lang-java";
import { enterInBlockComment, enterInDocComment, javaDocTags, phpDocTags } from "./docComment";
const views: EditorView[] = [];
afterEach(() => views.splice(0).forEach((v) => v.destroy()));
const slashStar = EditorState.languageData.of(() => [
{ commentTokens: { line: "//", block: { open: "/*", close: "*/" } } },
]);
const hash = EditorState.languageData.of(() => [{ commentTokens: { line: "#" } }]);
/** `|` marks the caret. */
function enter(text: string, language = "php", tokens = slashStar) {
const at = text.indexOf("|");
const view = new EditorView({
state: EditorState.create({
doc: text.replace("|", ""),
selection: EditorSelection.cursor(at),
extensions: [tokens, history()],
}),
});
views.push(view);
const handled = enterInDocComment(language)(view);
const s = view.state;
const caret = s.selection.main.head;
return { handled, view, text: s.doc.sliceString(0, caret) + "|" + s.doc.sliceString(caret) };
}
describe("Enter after /**", () => {
it("lists a PHP function's parameters and return type, caret on the summary line", () => {
const r = enter("/**|\nfunction f(int $a, $b): string {}\n/**\n * old\n */\nfunction g() {}");
expect(r.handled).toBe(true);
expect(r.text).toBe(
"/**\n * |\n * @param int $a\n * @param $b\n * @return string\n */\nfunction f(int $a, $b): string {}\n/**\n * old\n */\nfunction g() {}",
);
undo(r.view);
expect(r.view.state.doc.toString()).toBe(
"/**\nfunction f(int $a, $b): string {}\n/**\n * old\n */\nfunction g() {}",
);
});
it("uses the indentation of the /** line inside a class", () => {
expect(
enter("class A {\n /**|\n public static function make(?array $o = [], string ...$rest): ?static {}\n}")
.text,
).toBe(
"class A {\n /**\n * |\n * @param ?array $o\n * @param string ...$rest\n * @return ?static\n */\n public static function make(?array $o = [], string ...$rest): ?static {}\n}",
);
});
it("gives constructors and void methods no @return; promoted parameters keep their type", () => {
expect(phpDocTags("public function __construct(private int $a, #[Attr] readonly Foo&Bar $b) {}")).toEqual([
"@param int $a",
"@param Foo&Bar $b",
]);
expect(phpDocTags("function run(array &$x = array(1, 2), &$y): void {}")).toEqual(["@param array $x", "@param $y"]);
expect(phpDocTags("abstract protected function find(int|string $id): ?Model;")).toEqual([
"@param int|string $id",
"@return ?Model",
]);
});
it("closes the comment with an empty summary above anything else, and in other /* */ languages", () => {
expect(enter("/**|\nclass A {}").text).toBe("/**\n * |\n */\nclass A {}");
expect(enter(" /**|\n\nconst x = 1;", "javascript").text).toBe(" /**\n * |\n */\n\nconst x = 1;");
});
it("only breaks the line in an existing doc block, after other text, or without /* */ comments", () => {
expect(enter("/**|\n * old\n */\nfunction f() {}").handled).toBe(false);
expect(enter("$a = 1; /**|").handled).toBe(false);
expect(enter("/** x|").handled).toBe(false);
expect(enter("/**|", "python", hash).handled).toBe(false);
});
});
/** Enter through `enterInBlockComment`, in a parsed Java document. */
function enterBlock(text: string) {
const at = text.indexOf("|");
const view = new EditorView({
state: EditorState.create({
doc: text.replace("|", ""),
selection: EditorSelection.cursor(at),
extensions: [java()],
}),
});
views.push(view);
const handled = enterInBlockComment(view);
const s = view.state;
const caret = s.selection.main.head;
return { handled, text: s.doc.sliceString(0, caret) + "|" + s.doc.sliceString(caret) };
}
describe("Enter in a /* */ comment", () => {
it("starts the new line with a leading asterisk aligned under the comment's", () => {
expect(enterBlock("/**\n * xxxx|\n **/\nclass A {}").text).toBe("/**\n * xxxx\n * |\n **/\nclass A {}");
expect(enterBlock("class A {\n /**\n * a|b\n */\n}").text).toBe(
"class A {\n /**\n * a\n * |b\n */\n}",
);
expect(enterBlock("class A {\n /* note|\n */\n}").text).toBe("class A {\n /* note\n * |\n */\n}");
});
it("keeps the closing */ on its own line", () => {
expect(enterBlock("/** summary |*/\nclass A {}").text).toBe("/** summary\n * |\n */\nclass A {}");
});
it("leaves Enter alone outside a comment, before its asterisk, and in a string", () => {
expect(enterBlock("/* a */ int x;|").handled).toBe(false);
expect(enterBlock("/**\n| * a\n */").handled).toBe(false);
expect(enterBlock('class A { String s = "/* x|"; }').handled).toBe(false);
expect(enterBlock("// a /* b|").handled).toBe(false);
});
});
describe("Java documentation stub tags", () => {
it("lists a method's parameters, return value and exceptions", () => {
expect(javaDocTags("public String greet(String name, int times) {")).toEqual([
"@param name",
"@param times",
"@return",
]);
expect(
javaDocTags(
'@Override\n@SuppressWarnings({"a", "b"})\npublic static <K, V extends Comparable<V>> Map<K, List<V>> group(final Map<K, V> in, String... rest) throws IOException, java.sql.SQLException {',
),
).toEqual([
"@param in",
"@param rest",
"@param <K>",
"@param <V>",
"@return",
"@throws IOException",
"@throws java.sql.SQLException",
]);
});
it("gives void methods and constructors no @return", () => {
expect(javaDocTags("void run() {")).toEqual([]);
expect(javaDocTags("public App(int port, String[] args) {")).toEqual(["@param port", "@param args"]);
});
it("lists a generic type's parameters and a record's components", () => {
expect(javaDocTags("public final class Box<T extends Number> {")).toEqual(["@param <T>"]);
expect(javaDocTags("public record Point<T>(int x, T y) {")).toEqual(["@param x", "@param y", "@param <T>"]);
expect(javaDocTags("class Plain {")).toEqual([]);
});
it("finds nothing for fields and statements", () => {
expect(javaDocTags("private int count = 0;")).toEqual([]);
expect(javaDocTags("return foo(x);")).toEqual([]);
});
});