aboutsummaryrefslogtreecommitdiff
path: root/src/index.ts
blob: 690572b352116007900e48cd5fc1e55d16bec7aa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { parser } from "./syntax.grammar";
import {
  LRLanguage,
  LanguageSupport,
  indentNodeProp,
  foldNodeProp,
  foldInside,
  delimitedIndent,
} from "@codemirror/language";
import { highlighting } from "./highlight";
import { tabloidCompletion } from "./autocomplete";
import { autocompletion } from "@codemirror/autocomplete";

// Wrap parser to debug
const originalParse = parser.parse.bind(parser);
parser.parse = function (input, fragments, ranges) {
  console.log(
    "[PARSER] parse called with input:",
    typeof input === "string" ? input.substring(0, 100) : input,
  );
  const result = originalParse(input, fragments, ranges);
  console.log("[PARSER] parse result:", result.toString());
  return result;
};

export const tabloidLanguage = LRLanguage.define({
  parser: parser.configure({
    props: [
      indentNodeProp.add({
        Block: delimitedIndent({ closing: "endOfStory", align: false }),
        ParenBlock: delimitedIndent({ closing: ")", align: false }),
        FunctionDecl: (context) => context.baseIndent + context.unit,
        IfStatement: (context) => context.baseIndent + context.unit,
      }),
      foldNodeProp.add({
        Block: foldInside,
        ParenBlock: foldInside,
      }),
      highlighting,
    ],
  }),
  languageData: {
    commentTokens: {},
    closeBrackets: { brackets: ["(", '"', "'"] },
    autocomplete: tabloidCompletion,
  },
});

export function tabloid() {
  return new LanguageSupport(tabloidLanguage, [
    autocompletion({ override: [tabloidCompletion] }),
  ]);
}

export { tabloidCompletion };