當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。