From 901db807bfa33bccda089e28877d35684b2d4420 Mon Sep 17 00:00:00 2001 From: bchatard Date: Sun, 5 May 2019 12:50:47 +0200 Subject: [PATCH] :children_crossing: Add support for search engine customisation #10 --- README.md | 9 +++- .../product.md} | 0 doc/customisation/search.md | 21 ++++++++++ info.plist | 3 ++ src/index.js | 12 +----- src/search.js | 41 +++++++++++++++++++ 6 files changed, 74 insertions(+), 12 deletions(-) rename doc/{customisation.md => customisation/product.md} (100%) create mode 100644 doc/customisation/search.md create mode 100644 src/search.js diff --git a/README.md b/README.md index c6b820a..83c2058 100755 --- a/README.md +++ b/README.md @@ -75,10 +75,14 @@ For other versions: ## Customisation +- [Product](doc/customisation/product.md) + Some of JetBrains product are available in different editions (ex: PyCharm - Professional, Edu & Community). In that case they share the same `bin` / `keyword`, so you need to customise the _Preferences_ folder to retrieve your projects. -[Customisation](./doc/customisation.md) +- [Search](doc/customisation/search.md) + +A search engine is used to retrieve relevant project. For some use cases, the default configuration is not optimal for you. ## JetBrains Actions @@ -96,7 +100,8 @@ It's an early version - `jb_product_cache_lifetime`: cache lifetime in seconds for "product data" (application path, bin path etc) [default: 86400 seconds] - `jb_project_cache_lifetime`: cache lifetime in seconds for project list (one cache per app) [default: 3600 seconds] -- `jb_product_customisation_file`: path to JSON file for [customisation](#customisation) +- `jb_product_customisation_file`: path to JSON file for [product customisation](#customisation) +- `jb_search_customisation_file`: path to JSON file for [search customisation](#customisation) ## Changelog diff --git a/doc/customisation.md b/doc/customisation/product.md similarity index 100% rename from doc/customisation.md rename to doc/customisation/product.md diff --git a/doc/customisation/search.md b/doc/customisation/search.md new file mode 100644 index 0000000..cfab397 --- /dev/null +++ b/doc/customisation/search.md @@ -0,0 +1,21 @@ +# Search + +### How-to + +1. Create a JSON file somewhere +2. This JSON file need to follow the same structure of [Fuse.js](https://fusejs.io/). +3. Open the workflow, and go to the workflow configuration window (the icon like this [x]) +4. Add new environment variable: + - name: `jb_search_customisation_file` + - value: the path to your JSON file (relative to your home - ex: `/.config/alfred-jetbrains/search.json`) + +Example: + +```json +{ + "keys": [ + { "name": "title", "weight": 1 }, + { "name": "subtitle", "weight": 0.1 } + ] +} +``` diff --git a/info.plist b/info.plist index 7fb1ebe..eb1f904 100644 --- a/info.plist +++ b/info.plist @@ -936,12 +936,15 @@ fi 86400 jb_product_customisation_file + jb_search_customisation_file + jb_project_cache_lifetime 3600 variablesdontexport jb_product_customisation_file + jb_search_customisation_file version diff --git a/src/index.js b/src/index.js index 4125080..542e32c 100644 --- a/src/index.js +++ b/src/index.js @@ -29,16 +29,8 @@ if (items.length) { let matchItems = []; let matchTime; if (process.env.jb_enhanced_search) { - const fuseOption = { - shouldSort: true, - threshold: 0.4, - location: 0, - distance: 80, - maxPatternLength: 32, - minMatchCharLength: 2, - keys: [{ name: "title", weight: 1 }, { name: "subtitle", weight: 0.5 }] - }; - const fuse = new fuseEngine(items, fuseOption); + const searchOptions = require("search"); + const fuse = new fuseEngine(items, searchOptions); matchItems = fuse.search(query); matchTime = new Date(); } else { diff --git a/src/search.js b/src/search.js new file mode 100644 index 0000000..bdd26de --- /dev/null +++ b/src/search.js @@ -0,0 +1,41 @@ +const alfy = require("alfy"); +const fs = require("fs"); + +const searchOptions = () => { + let fuseOptions = { + shouldSort: true, + threshold: 0.4, + location: 0, + distance: 80, + maxPatternLength: 32, + minMatchCharLength: 2, + keys: [{ name: "title", weight: 1 }, { name: "subtitle", weight: 0.5 }] + }; + + if (process.env.jb_search_customisation_file) { + const customisationFilePath = path.join( + process.env.HOME, + process.env.jb_search_customisation_file + ); + try { + if (fs.existsSync(customisationFilePath)) { + const customSearchOptions = JSON.parse( + fs.readFileSync(customisationFilePath).toString() + ); + fuseOptions = { + ...fuseOptions, + ...customSearchOptions + }; + } + } catch (e) { + // die silently + if (alfy.debug) { + console.error(e); + } + } + } + + return fuseOptions; +}; + +export default searchOptions;