当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript languages.createDiagnosticCollection方法代码示例

本文整理汇总了TypeScript中vscode.languages.createDiagnosticCollection方法的典型用法代码示例。如果您正苦于以下问题:TypeScript languages.createDiagnosticCollection方法的具体用法?TypeScript languages.createDiagnosticCollection怎么用?TypeScript languages.createDiagnosticCollection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在vscode.languages的用法示例。


在下文中一共展示了languages.createDiagnosticCollection方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: activate


//.........这里部分代码省略.........
        if (token.isCancellationRequested) return null;

        let [status, result] = await session.request(['errors']);
        if (token.isCancellationRequested) return null;

        if (status !== 'return') return;

        let diagnostics = [];

        result.map(({type, start, end, message}) => {
            let fromType = (type) => {
                switch (type) {
                    case 'type':
                    case "parser":
                    case "env":
                    case "unknown":
                        return vscode.DiagnosticSeverity.Error;
                    case "warning":
                        return vscode.DiagnosticSeverity.Warning;
                }
            };

            if (type === 'type' &&
                message.startsWith('Error: Signature mismatch:') &&
                message.includes(': Actual declaration')) {
                let regex = /^\s*File ("[^"]+"), line (\d+), characters (\d+)-(\d+): Actual declaration$/mg;
                for (let match; (match = regex.exec(message)) !== null;) {
                    let file = JSON.parse(match[1]);
                    let line = JSON.parse(match[2]);
                    let col1 = JSON.parse(match[3]);
                    let col2 = JSON.parse(match[4]);

                    if (Path.basename(file) === Path.basename(document.fileName)) {
                        diagnostics.push(
                            new vscode.Diagnostic(
                                toVsRange({ line, col: col1 }, { line, col: col2 }),
                                message,
                                fromType(type.toLowerCase())
                            )
                        );
                    } else {
                        // Log here?
                    }
                }
                return;
            }

            diagnostics.push(
                new vscode.Diagnostic(
                    toVsRange(start, end),
                    message,
                    fromType(type.toLowerCase())
                )
            );
        });

        return diagnostics;
    };

    let LINTER_DEBOUNCE_TIMER = Symbol();
    let LINTER_TOKEN_SOURCE = Symbol();

    let diagnosticCollection = vscode.languages.createDiagnosticCollection('ocaml');

    let lintDocument = (document: vscode.TextDocument) => {
        if (document.languageId !== 'ocaml') return;

        clearTimeout(document[LINTER_DEBOUNCE_TIMER]);
        document[LINTER_DEBOUNCE_TIMER] = setTimeout(async () => {
            if (document[LINTER_TOKEN_SOURCE]) {
                document[LINTER_TOKEN_SOURCE].cancel();
            }
            document[LINTER_TOKEN_SOURCE] = new vscode.CancellationTokenSource();

            let diagnostics = await provideLinter(document, document[LINTER_TOKEN_SOURCE].token);
            diagnosticCollection.set(document.uri, diagnostics);
        }, configuration.get<number>('lintDelay'));
    };

    vscode.workspace.onDidChangeTextDocument(({document}) => {
        if (document.languageId === 'ocaml') {
            lintDocument(document);
            return;
        }

        let relintOpenedDocuments = () => {
            diagnosticCollection.clear();
            for (let document of vscode.workspace.textDocuments) {
                if (document.languageId === 'ocaml') {
                    lintDocument(document);
                }
            }
        };

        let path = Path.basename(document.fileName);
        if (path === '.merlin') {
            relintOpenedDocuments();
        }
    });
}
开发者ID:db4,项目名称:vscode-ocaml,代码行数:101,代码来源:extension.ts


注:本文中的vscode.languages.createDiagnosticCollection方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。