欢迎去往我的github

vscode语法校验器

笔记软件在2023/5/6 11:08:57推送该笔记

// 文本文件的内容改变时。文档首次打开或者文档内容修改时会触发这个事件。
documents.onDidChangeContent(async change => {
  let textDocument = change.document;
  // 这个简单示例中,每次校验时我们都获取一次设置
  let settings = await getDocumentSettings(textDocument.uri);

  // 校验器会检查所有的大写单词是否超过 2 个字母
  let text = textDocument.getText();
  let pattern = /\b[A-Z]{2,}\b/g;
  let m: RegExpExecArray | null;

  let problems = 0;
  let diagnostics: Diagnostic[] = [];
  while ((m = pattern.exec(text)) && problems < settings.maxNumberOfProblems) {
    problems++;
    let diagnostic: Diagnostic = {
      severity: DiagnosticSeverity.Warning,
      range: {
        start: textDocument.positionAt(m.index),
        end: textDocument.positionAt(m.index + m[0].length)
      },
      message: `${m[0]} is all uppercase.`,
      source: 'ex'
    };
    if (hasDiagnosticRelatedInformationCapability) {
      diagnostic.relatedInformation = [
        {
          location: {
            uri: textDocument.uri,
            range: Object.assign({}, diagnostic.range)
          },
          message: 'Spelling matters'
        },
        {
          location: {
            uri: textDocument.uri,
            range: Object.assign({}, diagnostic.range)
          },
          message: 'Particularly for names'
        }
      ];
    }
    diagnostics.push(diagnostic);
  }

  // 将诊断信息发送给 VS Code
  connection.sendDiagnostics({ uri: textDocument.uri, diagnostics });
});

posted @ 2023-05-06 11:09  月夜魂归  阅读(18)  评论(0编辑  收藏  举报
……