Skip to content

Commit

Permalink
🚸 Add support for search engine customisation
Browse files Browse the repository at this point in the history
  • Loading branch information
bchatard committed May 5, 2019
1 parent a962de3 commit 901db80
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 12 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
File renamed without changes.
21 changes: 21 additions & 0 deletions doc/customisation/search.md
Original file line number Diff line number Diff line change
@@ -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 }
]
}
```
3 changes: 3 additions & 0 deletions info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -936,12 +936,15 @@ fi</string>
<string>86400</string>
<key>jb_product_customisation_file</key>
<string></string>
<key>jb_search_customisation_file</key>
<string></string>
<key>jb_project_cache_lifetime</key>
<string>3600</string>
</dict>
<key>variablesdontexport</key>
<array>
<string>jb_product_customisation_file</string>
<string>jb_search_customisation_file</string>
</array>
<key>version</key>
<string></string>
Expand Down
12 changes: 2 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
41 changes: 41 additions & 0 deletions src/search.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 901db80

Please sign in to comment.