aboutsummaryrefslogtreecommitdiffstats
path: root/atk16_syntax/extension.js
blob: 26b4d9d0a958d30569c566f382b51a2b73b5d1b3 (plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
const vscode = require('vscode');
const path = require('path');

class ATK16HoverProvider {
  async provideHover(document, position, token) {
    const range = document.getWordRangeAtPosition(position, /[\w-/.]+/);
    const labelText = document.getText(range);
    const subroutineDefinition = await findSubroutineInDocument(document, labelText);
    if (subroutineDefinition) {
      const hoverText = new vscode.MarkdownString(subroutineDefinition);
      hoverText.isTrusted = true;
      return new vscode.Hover(hoverText);
    }

    // Search for labels in included files
    for (let i = 0; i < document.lineCount; i++) {
      const line = document.lineAt(i);
      const includeMatch = line.text.match(/@include\s+(\S+)/);
      if (includeMatch) {
        const includedFilePath = path.join(
          path.dirname(document.fileName),
          `${includeMatch[1]}.atk16`
        );

        if (await vscode.workspace.fs.stat(vscode.Uri.file(includedFilePath))) {
          const includedDocument = await vscode.workspace.openTextDocument(
            includedFilePath
          );
          const subroutineDefinition = await findSubroutineInDocument(includedDocument, labelText);
          if (subroutineDefinition) {
            const hoverText = new vscode.MarkdownString(subroutineDefinition);
            hoverText.isTrusted = true;
            return new vscode.Hover(hoverText);
          }
        }
      }
    }

    return null;
  }
}

class ATK16DefinitionProvider {
  async provideDefinition(document, position, token) {
    const range = document.getWordRangeAtPosition(position, /[\w-/.]+/);
    const text = document.getText(range);

    const line = document.lineAt(position);
    const includeMatch = line.text.match(/@include\s+(\S+)/);
    const useMatch = line.text.match(/@use\s+(\S+):/);

    if (includeMatch && includeMatch[1] === text) {
      const includedFilePath = path.join(
        path.dirname(document.fileName),
        `${includeMatch[1]}.atk16`
      );

      try {
        await vscode.workspace.fs.stat(vscode.Uri.file(includedFilePath));
        return new vscode.Location(
          vscode.Uri.file(includedFilePath),
          new vscode.Position(0, 0)
        );
      } catch (err) {
        vscode.window.showErrorMessage(
          `ATK16: File not found: ${includedFilePath}`
        );
      }
    } else if (useMatch && useMatch[1] === text) {
      const includedFilePath = path.join(
        path.dirname(document.fileName),
        `${useMatch[1]}.py`
      );

      try {
        await vscode.workspace.fs.stat(vscode.Uri.file(includedFilePath));
        return new vscode.Location(
          vscode.Uri.file(includedFilePath),
          new vscode.Position(0, 0)
        );
      } catch (err) {
        vscode.window.showErrorMessage(
          `ATK16: File not found: ${includedFilePath}`
        );
      }
    } else {
      const labelText = document.getText(range);
      let definitionLocation = await findDefinitionInDocument(document, labelText);
      if (definitionLocation) {
        return definitionLocation;
      }

      // Search for labels in included files
      for (let i = 0; i < document.lineCount; i++) {
        const line = document.lineAt(i);
        const includeMatch = line.text.match(/@include\s+(\S+)/);
        if (includeMatch) {
          const includedFilePath = path.join(
            path.dirname(document.fileName),
            `${includeMatch[1]}.atk16`
          );

          if (await vscode.workspace.fs.stat(vscode.Uri.file(includedFilePath))) {
            const includedDocument = await vscode.workspace.openTextDocument(
              includedFilePath
            );
            definitionLocation = await findDefinitionInDocument(includedDocument, labelText);
            if (definitionLocation) {
              return definitionLocation;
            }
          }
        }
      }
    }

    return null;
  }
}

async function findDefinitionInDocument(document, labelText) {
  for (let i = 0; i < document.lineCount; i++) {
    const line = document.lineAt(i);
    if (line.text.includes(`@label ${labelText}`) || line.text.includes(`@let ${labelText}`)) {
      return new vscode.Location(document.uri, line.range.start);
    }
  }
  return null;
}

async function findSubroutineInDocument(document, labelText) {
  let name = null;
  const params = [];
  const returns = [];
  let extra = "";
  for (let i = 0; i < document.lineCount; i++) {
    const line = document.lineAt(i);
    if (line.text.includes(`@label ${labelText}`)) {
      for (let j = i + 1; j < document.lineCount; j++) {
        const docLine = document.lineAt(j);
        const docCommentMatch = docLine.text.match(/;;;\s+(.*)/);
        if (docCommentMatch) {
          const lineText = docCommentMatch[1];
          const parts = lineText.trim().split(/\s+/g);
          if (lineText.startsWith('subroutine')) {
             name = parts[parts.length - 1].trim();
          } else if (lineText.startsWith('param')) {
            const [_kw, reg, ...desc] = parts;
            params.push([reg.trim(), desc.filter(a => a).join(" ")])
          } else if (lineText.startsWith('return')) {
            const [_kw, reg, ...desc] = parts;
            returns.push([reg.trim(), desc.filter(a => a).join(" ")])
          } else {
            extra += `${lineText}\n`;
          }
        } else {
          break;
        }
      }
    }
  }

  if (name === null) return null;

  let definition = `### ${name}\n_Subroutine_\n\n`;
  definition += extra;
  if (params.length > 0) {
    definition += "\n\n|Reg|Parameter|\n";
    definition += "|:---|:---|\n";
  }
  for (const [reg, desc] of params) {
    definition += `|${reg}|${desc}|\n`;
  }

  if (returns.length > 0) {
    definition += "\n|Reg|Return|\n";
    definition += "|:---|:---|\n";
  }
  for (const [reg, desc] of returns) {
    definition += `|${reg}|${desc}|\n`;
  }
  return definition;
}

function activate(context) {
  context.subscriptions.push(
    vscode.languages.registerDefinitionProvider('atk16', new ATK16DefinitionProvider()),
    vscode.languages.registerHoverProvider('atk16', new ATK16HoverProvider())
  );
}

function deactivate() {}

module.exports = {
  activate,
  deactivate,
};