Skip to content

Commit

Permalink
Warp up prettier-eslint worker with first available open document (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
netux authored Dec 14, 2023
1 parent aeaab5f commit 5c3f430
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@
"code",
"review"
]
},
{
"login": "netux",
"name": "Martín Rodríguez",
"avatar_url": "https://avatars.githubusercontent.com/u/6181929?v=4",
"profile": "https://netux.site/",
"contributions": [
"code"
]
}
],
"contributorsPerLine": 7
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<img src="https://github.com/idahogurl/vs-code-prettier-eslint/blob/master/icon.png?raw=true" width="150">

<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-4-orange.svg?style=flat-square)](#contributors-)
[![All Contributors](https://img.shields.io/badge/all_contributors-5-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END -->

# [VS Code Prettier ESLint](https://marketplace.visualstudio.com/items?itemName=rvest.vs-code-prettier-eslint)
Expand Down Expand Up @@ -168,6 +168,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<td align="center" valign="top" width="14.28%"><a href="https://github.com/tbekaert"><img src="https://avatars.githubusercontent.com/u/11920484?v=4?s=100" width="100px;" alt="Thomas Bekaert"/><br /><sub><b>Thomas Bekaert</b></sub></a><br /><a href="https://github.com/idahogurl/vs-code-prettier-eslint/commits?author=tbekaert" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://mattbrannon.dev/"><img src="https://avatars.githubusercontent.com/u/36570183?v=4?s=100" width="100px;" alt="Matt Brannon"/><br /><sub><b>Matt Brannon</b></sub></a><br /><a href="https://github.com/idahogurl/vs-code-prettier-eslint/commits?author=mattbrannon" title="Documentation">📖</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://www.1stg.me/"><img src="https://avatars.githubusercontent.com/u/8336744?v=4?s=100" width="100px;" alt="JounQin"/><br /><sub><b>JounQin</b></sub></a><br /><a href="https://github.com/idahogurl/vs-code-prettier-eslint/commits?author=JounQin" title="Code">💻</a> <a href="https://github.com/idahogurl/vs-code-prettier-eslint/pulls?q=is%3Apr+reviewed-by%3AJounQin" title="Reviewed Pull Requests">👀</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://netux.site/"><img src="https://avatars.githubusercontent.com/u/6181929?v=4?s=100" width="100px;" alt="Martín Rodríguez"/><br /><sub><b>Martín Rodríguez</b></sub></a><br /><a href="https://github.com/idahogurl/vs-code-prettier-eslint/commits?author=netux" title="Code">💻</a></td>
</tr>
</tbody>
</table>
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
"onLanguage:typescript",
"onLanguage:typescriptreact",
"onLanguage:vue",
"onLanguage:yaml"
"onLanguage:yaml",
"onStartupFinished"
],
"scripts": {
"lint": "eslint . --ignore-pattern test/fixtures/",
Expand Down
47 changes: 47 additions & 0 deletions src/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,44 @@ function formatter(document) {
}
}

/**
* Provides a promise that resolves as soon as there is an active text editor
* with a document open on a language this extension supports.
*
* @returns {Promise<TextDocument>}
*/
function waitForActiveSupportedDocument() {
if (!window.activeTextEditor || !supportedLanguages.includes(window.activeTextEditor.document.languageId)) {

Check warning on line 81 in src/extension.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

This line has a length of 110. Maximum allowed is 100

Check failure on line 81 in src/extension.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

'supportedLanguages' was used before it was defined
return new Promise((resolve) => {
const handler = window.onDidChangeActiveTextEditor(({ document }) => {
if (supportedLanguages.includes(document.languageId)) {

Check failure on line 84 in src/extension.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

'supportedLanguages' was used before it was defined
handler.dispose();
resolve(document);
}
});
})

Check failure on line 89 in src/extension.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Missing semicolon
}

return Promise.resolve(window.activeTextEditor.document);
}

/**
* Warms up the worker by running 'prettier-eslint' with some dummy data.
*
* @param {TextDocument} document - Used to resolve 'prettier-eslint', as well as needed
* by 'prettier-eslint' to resolve its internal dependencies (eslint and prettier).
*/
async function warmUpWorker(document) {
const prettierEslintPath = getModulePath(document.fileName, 'prettier-eslint');

formatText({
text: '',
prettierEslintPath,
filePath: document.fileName,
});
}


Check failure on line 111 in src/extension.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

More than 1 blank line not allowed
const supportedLanguages = [
'css',
'graphql',
Expand All @@ -97,3 +135,12 @@ supportedLanguages.forEach((language) => {
},
});
});

waitForActiveSupportedDocument()
.then((document) => warmUpWorker(document))
.then(() => {
outputChannel.appendLine('Worker has been warmed up');
})
.catch((error) => {
outputChannel.appendLine('Error: Could not warm up worker. Formatting a file for the first time may take longer than usual.\nStacktrace: ' + error.stack);

Check failure on line 145 in src/extension.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Expected indentation of 4 spaces but found 6

Check failure on line 145 in src/extension.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Unexpected string concatenation
});

0 comments on commit 5c3f430

Please sign in to comment.