minor adjustment to readme
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / lib / errorHandler.js
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 const coc_nvim_1 = require("coc.nvim");
4 let outputChannel;
5 /**
6  * Adds the filepath to the error message
7  *
8  * @param msg The original error message
9  * @param fileName The path to the file
10  * @returns {string} enhanced message with the filename
11  */
12 function addFilePath(msg, fileName) {
13     const lines = msg.split('\n');
14     if (lines.length > 0) {
15         lines[0] = lines[0].replace(/(\d*):(\d*)/g, `${fileName}:$1:$2`);
16         return lines.join('\n');
17     }
18     return msg;
19 }
20 /**
21  * Append messages to the output channel and format it with a title
22  *
23  * @param message The message to append to the output channel
24  */
25 function addToOutput(message, type = 'Trace') {
26     if (!outputChannel)
27         return;
28     const title = `${type} - ${new Date().toLocaleString()}:`;
29     // Create a sort of title, to differentiate between messages
30     outputChannel.appendLine('');
31     // Append actual output
32     outputChannel.appendLine(`[${title}] ${message}\n`);
33 }
34 exports.addToOutput = addToOutput;
35 /**
36  * Execute a callback safely, if it doesn't work, return default and log messages.
37  *
38  * @param cb The function to be executed,
39  * @param defaultText The default value if execution of the cb failed
40  * @param fileName The filename of the current document
41  * @returns {string} formatted text or defaultText
42  */
43 function safeExecution(cb, defaultText, fileName) {
44     if (cb instanceof Promise) {
45         return cb
46             .then(returnValue => {
47             // updateStatusBar('Prettier: $(check)')
48             return returnValue;
49         })
50             .catch((err) => {
51             addToOutput(addFilePath(err.message, fileName), 'Error');
52             // updateStatusBar('Prettier: $(x)')
53             return defaultText;
54         });
55     }
56     try {
57         const returnValue = cb();
58         // updateStatusBar('Prettier: $(check)')
59         return returnValue;
60     }
61     catch (err) {
62         addToOutput(addFilePath(err.message, fileName), 'Error');
63         // updateStatusBar('Prettier: $(x)')
64         return defaultText;
65     }
66 }
67 exports.safeExecution = safeExecution;
68 /**
69  * Setup the output channel and the statusBarItem.
70  * Create a command to show the output channel
71  *
72  * @returns {Disposable} The command to open the output channel
73  */
74 function setupErrorHandler() {
75     // Setup the outputChannel
76     outputChannel = coc_nvim_1.workspace.createOutputChannel('prettier');
77     return coc_nvim_1.commands.registerCommand('prettier.open-output', () => {
78         outputChannel.show();
79     });
80 }
81 exports.setupErrorHandler = setupErrorHandler;
82 //# sourceMappingURL=errorHandler.js.map