Skip to content

web search done right

Optics LSP Chores (#145)

Oliver Bøvingf5bb0d2ef8a2
diff --git a/.vscode/settings.json b/.vscode/settings.json
index d10c3861..b3ae69bc 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -20,7 +20,7 @@
"tailwindCSS.experimental.configFile": "frontend/tailwind.config.ts",
"rust-analyzer.linkedProjects": [
"Cargo.toml",
- "optics-lsp/Cargo.toml",
+ "./crates/optics-lsp/Cargo.toml",
"./crates/core/Cargo.toml"
]
}
diff --git a/crates/optics-lsp/Cargo.lock b/crates/optics-lsp/Cargo.lock
index be4d1259..cf57baaa 100644
--- a/crates/optics-lsp/Cargo.lock
+++ b/crates/optics-lsp/Cargo.lock
@@ -255,6 +255,15 @@ dependencies = [
"either",
]
+[[package]]
+name = "itertools"
+version = "0.12.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569"
+dependencies = [
+ "either",
+]
+
[[package]]
name = "itoa"
version = "1.0.6"
@@ -353,7 +362,7 @@ dependencies = [
"proc-macro2",
"quote",
"regex-syntax 0.6.28",
- "syn 2.0.28",
+ "syn 2.0.48",
]
[[package]]
@@ -367,9 +376,9 @@ dependencies = [
[[package]]
name = "lsp-types"
-version = "0.93.2"
+version = "0.95.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "9be6e9c7e2d18f651974370d7aff703f9513e0df6e464fd795660edc77e6ca51"
+checksum = "158c1911354ef73e8fe42da6b10c0484cb65c7f1007f28022e847706c1ab6984"
dependencies = [
"bitflags 1.3.2",
"serde",
@@ -415,6 +424,7 @@ name = "optics-lsp"
version = "0.1.0"
dependencies = [
"console_error_panic_hook",
+ "itertools 0.12.1",
"js-sys",
"lsp-types",
"optics",
@@ -510,18 +520,18 @@ dependencies = [
[[package]]
name = "proc-macro2"
-version = "1.0.66"
+version = "1.0.78"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9"
+checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
-version = "1.0.32"
+version = "1.0.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "50f3b39ccfb720540debaa0164757101c08ecb8d326b15358ce76a62c7e85965"
+checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef"
dependencies = [
"proc-macro2",
]
@@ -602,18 +612,18 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "serde"
-version = "1.0.154"
+version = "1.0.196"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "8cdd151213925e7f1ab45a9bbfb129316bd00799784b174b7cc7bcd16961c49e"
+checksum = "870026e60fa08c69f064aa766c10f10b1d62db9ccd4d0abb206472bee0ce3b32"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde-wasm-bindgen"
-version = "0.4.5"
+version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e3b4c031cd0d9014307d82b8abf653c0290fbdaeb4c02d00c63cf52f728628bf"
+checksum = "b9b713f70513ae1f8d92665bbbbda5c295c2cf1da5542881ae5eefe20c9af132"
dependencies = [
"js-sys",
"serde",
@@ -622,13 +632,13 @@ dependencies = [
[[package]]
name = "serde_derive"
-version = "1.0.154"
+version = "1.0.196"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "4fc80d722935453bcafdc2c9a73cd6fac4dc1938f0346035d84bf99fa9e33217"
+checksum = "33c85360c95e7d137454dc81d9a4ed2b8efd8fbe19cee57357b32b9771fccb67"
dependencies = [
"proc-macro2",
"quote",
- "syn 1.0.109",
+ "syn 2.0.48",
]
[[package]]
@@ -691,9 +701,9 @@ dependencies = [
[[package]]
name = "syn"
-version = "2.0.28"
+version = "2.0.48"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "04361975b3f5e348b2189d8dc55bc942f278b2d482a6a0365de5bdd62d351567"
+checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f"
dependencies = [
"proc-macro2",
"quote",
@@ -816,7 +826,7 @@ dependencies = [
"proc-macro2",
"quote",
"regex",
- "syn 2.0.28",
+ "syn 2.0.48",
]
[[package]]
@@ -833,9 +843,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "wasm-bindgen"
-version = "0.2.84"
+version = "0.2.91"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "31f8dcbc21f30d9b8f2ea926ecb58f6b91192c17e9d33594b3df58b2007ca53b"
+checksum = "c1e124130aee3fb58c5bdd6b639a0509486b0338acaaae0c84a5124b0f588b7f"
dependencies = [
"cfg-if",
"wasm-bindgen-macro",
@@ -843,24 +853,24 @@ dependencies = [
[[package]]
name = "wasm-bindgen-backend"
-version = "0.2.84"
+version = "0.2.91"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "95ce90fd5bcc06af55a641a86428ee4229e44e07033963a2290a8e241607ccb9"
+checksum = "c9e7e1900c352b609c8488ad12639a311045f40a35491fb69ba8c12f758af70b"
dependencies = [
"bumpalo",
"log",
"once_cell",
"proc-macro2",
"quote",
- "syn 1.0.109",
+ "syn 2.0.48",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
-version = "0.2.84"
+version = "0.2.91"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "4c21f77c0bedc37fd5dc21f897894a5ca01e7bb159884559461862ae90c0b4c5"
+checksum = "b30af9e2d358182b5c7449424f017eba305ed32a7010509ede96cdc4696c46ed"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
@@ -868,22 +878,22 @@ dependencies = [
[[package]]
name = "wasm-bindgen-macro-support"
-version = "0.2.84"
+version = "0.2.91"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2aff81306fcac3c7515ad4e177f521b5c9a15f2b08f4e32d823066102f35a5f6"
+checksum = "642f325be6301eb8107a83d12a8ac6c1e1c54345a7ef1a9261962dfefda09e66"
dependencies = [
"proc-macro2",
"quote",
- "syn 1.0.109",
+ "syn 2.0.48",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
-version = "0.2.84"
+version = "0.2.91"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "0046fef7e28c3804e5e38bfa31ea2a0f73905319b677e57ebe37e49358989b5d"
+checksum = "4f186bd2dcf04330886ce82d6f33dd75a7bfcf69ecf5763b89fcde53b6ac9838"
[[package]]
name = "winapi"
diff --git a/crates/optics-lsp/Cargo.toml b/crates/optics-lsp/Cargo.toml
index 780bbb2c..d351ab43 100644
--- a/crates/optics-lsp/Cargo.toml
+++ b/crates/optics-lsp/Cargo.toml
@@ -7,13 +7,14 @@ edition = "2021"
crate-type = ["cdylib"]
[dependencies]
-wasm-bindgen = "0.2.83"
-lsp-types = "0.93.2"
-js-sys = "0.3.60"
console_error_panic_hook = "0.1.7"
+itertools = "0.12.1"
+js-sys = "0.3.60"
+lsp-types = "0.95.0"
serde = { version = "1.0.137", features = ["rc", "derive"] }
-serde-wasm-bindgen = "0.4.5"
+serde-wasm-bindgen = "0.6.3"
thiserror = "1.0.31"
+wasm-bindgen = "0.2.83"
optics = { path = "../optics" }
diff --git a/crates/optics-lsp/LICENSE.md b/crates/optics-lsp/LICENSE.md
deleted file mode 120000
index 7eabdb1c..00000000
--- a/crates/optics-lsp/LICENSE.md
+++ /dev/null
@@ -1 +0,0 @@
-../LICENSE.md
\ No newline at end of file
diff --git a/crates/optics-lsp/LICENSE.md b/crates/optics-lsp/LICENSE.md
new file mode 100644
index 00000000..595efa21
--- /dev/null
+++ b/crates/optics-lsp/LICENSE.md
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2024 Stract ApS
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/crates/optics-lsp/package.json b/crates/optics-lsp/package.json
index 75034e11..11914a8e 100644
--- a/crates/optics-lsp/package.json
+++ b/crates/optics-lsp/package.json
@@ -1,6 +1,6 @@
{
"name": "Optics",
- "version": "0.0.11",
+ "version": "0.0.12",
"engines": {
"vscode": "^1.73.0"
},
@@ -57,4 +57,4 @@
}
]
}
-}
\ No newline at end of file
+}
diff --git a/crates/optics-lsp/src/docs.rs b/crates/optics-lsp/src/docs.rs
new file mode 100644
index 00000000..d7c1b8ad
--- /dev/null
+++ b/crates/optics-lsp/src/docs.rs
@@ -0,0 +1,96 @@
+pub fn token_docs(token: &optics::Token) -> Option<&'static str> {
+ Some(match token {
+ optics::Token::DiscardNonMatching => "All results that does not match any of the rules in the optic will be discarded.",
+
+ optics::Token::Rule => "A rule specifies how a particular search result should be treated. \
+ It consists of a `Matches` block and an optional `Action`. Any search result that matches the `Matches` block \
+ will have the `Action` applied to it. The action can either `Boost`, `Downrank` or `Discard` a result. An empty `Action` is \
+ equivalent to a `Boost` of 0.",
+
+ optics::Token::RankingPipeline => "The final ranking consists of multiple stages in a pipeline. Each stage receives the
+ best scoring webpages from the stage before it, and uses more accurate yet computationally expensive algorithms to rank
+ the pages for the next stage.",
+
+ optics::Token::Stage => "The final ranking consists of multiple stages in a pipeline. Each stage has different fields and
+ signals available to it.",
+
+ optics::Token::Matches => "`Matches` dictates the set of criteria a search result should match in order to have the action applied to it. \
+ A search result must match all the parts of the `Matches` block in order to match the specific rule.",
+
+ optics::Token::Site => "`Site(\"...\")` matches any search result where the pattern defined in `\"...\"` matches the site of the result. \
+ Note that when `Site` is used inside `Like` or `Dislike`, the pattern can only contain simple terms (no `*` and `|`). \n\n\
+ When the site is used in a `Matches` block, you can use `*` as a wildcard term and `|` to indicate either the end or start of a string. \n\
+ Consider the pattern `\"|sub.*.com|\"`. This will ensure that the terms `sub` and `.` must appear at the beggining of the site, then followed by any \
+ domain-name that ends in `.` and `com`. Note that `|` can only be used in the beggining or end of a pattern and the pattern will only match full terms (no substring matching). \n\n\
+ This example illustrates the difference between `Domain`, `Site` and `Url`:\n\
+ Assume a search result has the url `https://sub.example.org/page`. the domain here is `example.org`, the site is `sub.example.org` and the url is the entire url (with protocol).\
+ ",
+
+ optics::Token::Url => "`Url(\"...\")` matches any search result where the pattern defined in `\"...\"` matches the url of the result. \
+ You can use `*` as a wildcard term and `|` to indicate either the end or start of a url. \n\
+ Consider the pattern `\"https://sub.*.com|\"`. This will ensure that the terms `https`, `:`, `/`, `/`, `sub` and `.` must appear before any term that ends with `.` and `com` \
+ in the url. Note that `|` can only be used in the beggining or end of a pattern and the pattern will only match full terms (no substring matching). \n\n\
+ This example illustrates the difference between `Domain`, `Site` and `Url`:\n\
+ Assume a search result has the url `https://sub.example.org/page`. the domain here is `example.org`, the site is `sub.example.org` and the url is the entire url (with protocol).\
+ ",
+
+ optics::Token::Domain => "`Domain(\"...\")` matches any search result where the pattern defined in `\"...\"` matches the domain of the result. \
+ You can use `*` as a wildcard term and `|` to indicate either the end or start of a domain. \n\
+ Consider the pattern `\"example.org\"`. This is equivalent to doing a phrase search for `\"example.org\"` in the domain. Note that the pattern will only match full terms (no substring matching). \n\n\
+ This example illustrates the difference between `Domain`, `Site` and `Url`:\n\
+ Assume a search result has the url `https://sub.example.org/page`. the domain here is `example.org`, the site is `sub.example.org` and the url is the entire url (with protocol).\
+ ",
+
+ optics::Token::Title => "`Title(\"...\")` matches any search result where the pattern defined in `\"...\"` matches the title of the web page. \
+ You can use `*` as a wildcard term and `|` to indicate either the end or start of a title. \n\
+ Consider the pattern `\"|Best * ever\"`. This will match any result where the title starts with `Best` followed by any term(s) and then followed by the term `ever`. \
+ Note that the pattern will only match full terms (no substring matching) and the modifier `|` can only be used at the end or beggining of the pattern.",
+
+ optics::Token::Description => "`Description(\"...\")` matches any search result where the pattern defined in `\"...\"` matches the description of the web page. \
+ You can use `*` as a wildcard term and `|` to indicate either the end or start of a description. \n\
+ Consider the pattern `\"|Best * ever\"`. This will match any result where the description starts with `Best` followed by any term(s) and then followed by the term `ever`. \
+ Note that the pattern will only match full terms (no substring matching) and the modifier `|` can only be used at the end or beggining of the pattern.",
+
+ optics::Token::Content => "`Content(\"...\")` matches any search result where the pattern defined in `\"...\"` matches the content of the web page. \
+ The content of a webpage is all the text that is not part of navigational menues, footers etc. \n\
+ You can use `*` as a wildcard term and `|` to indicate either the end or start of the content. \n\
+ Consider the pattern `\"Best * ever\"`. This will match any result where the description starts with `Best` followed by any term(s) and then followed by the term `ever`. \
+ Note that the pattern will only match full terms (no substring matching) and the modifier `|` can only be used at the end or beggining of the pattern.",
+
+ optics::Token::MicroformatTag => "`MicroformatTag(\"...\")` matches any search result that contains the microformat tag defined in `\"...\"`. \
+ This is useful when looking for indieweb pages.",
+
+ optics::Token::Schema => "`Schema(\"...\")` matches any search result that contains the https://schema.org entity defined in `\"...\"`. \
+ As an example, `Schema(\"BlogPosting\")` matches all pages that contains the https://schema.org/BlogPosting entity. Note that `Schema` \
+ does not support the pattern syntax, but only simple strings.",
+
+ optics::Token::Ranking => "When results are ranked we take a weighted sum of various signals to give each webpage a score for the specific query. \
+ The top scored results are then presented to the user. `Ranking` allows you to alter the weight of all the `Signal`s and text `Field`s.",
+
+ optics::Token::Signal => "During ranking of the search results, a number of signals are combined in a weighted sum to create the final score for each search result. \
+ `Signal` allows you to change the coefficient used for each signal, and thereby alter the search result ranking. Some supported signals are e.g. \"host_centrality\", \"bm25\" and \"tracker_score\". \
+ A complete list of the available signals can be found in the code (https://github.com/StractOrg/Stract/blob/main/src/ranking/signal.rs)",
+
+ optics::Token::Field => "`Field` lets you change how the various text fields are prioritized during ranking (e.g. a search result matching text in the title is probably more relevant than a result where only the body matches). \
+ Some supported fields are e.g. \"title\", \"body\", \"backlink_text\" and \"site\". \
+ A complete list of available fields can be seen in the code (https://github.com/StractOrg/Stract/blob/main/src/schema.rs)",
+
+ optics::Token::Action => "`Action` defines which action should be applied to the matching search result. The result can either be boosted, downranked or discarded.",
+
+ optics::Token::Boost => "`Boost(...)` boosts the search result by the number specified in `...`.",
+
+ optics::Token::Downrank => "`Downrank(...)` downranks the search result by the number specified in `...`. A higher number further downranks the search result.",
+
+ optics::Token::Discard => "`Discard` discards the matching search result completely from the results page.",
+
+ optics::Token::Like => "`Like(Site(...))` lets you like specific hosts. During ranking, we will calculate a centrality meassure from all you liked sites \
+ so results that are heavily linked to from your liked sites will be favored. Note therefore, that `Like` not only alters the ranking of the specifc site, \
+ but also sites that are heavily linked to from the liked site.",
+
+ optics::Token::Dislike => "`Dislike(Site(...))` lets you dislike specifc hosts. During ranking, we will calculate a centrality meassure from all you dislike sites \
+ so results that are heavily linked to from your disliked sites will be downranked. Note therefore, that `Dislike` not only alters the ranking of the specifc site, \
+ but also sites that are heavily linked to from the disliked site.",
+
+ _ => return None,
+ })
+}
diff --git a/crates/optics-lsp/src/lib.rs b/crates/optics-lsp/src/lib.rs
index 18cc81e8..705d5b8b 100644
--- a/crates/optics-lsp/src/lib.rs
+++ b/crates/optics-lsp/src/lib.rs
@@ -14,12 +14,16 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
+mod docs;
+
use std::collections::HashMap;
+use itertools::Itertools;
use lsp_types::{
notification::{DidChangeTextDocument, DidOpenTextDocument, Notification},
Diagnostic, DiagnosticSeverity, DidChangeTextDocumentParams, DidOpenTextDocumentParams, Hover,
- HoverContents, HoverParams, MarkedString, Position, PublishDiagnosticsParams, Range, Url,
+ HoverContents, HoverParams, LanguageString, MarkedString, Position, PublishDiagnosticsParams,
+ Range, Url,
};
use optics::Optic;
use thiserror::Error;
@@ -144,106 +148,15 @@ impl OpticsBackend {
.and_then(|offset| {
file.token_at_offset(offset)
.and_then(|(start, token, end)| {
- let msg = match token {
- optics::Token::DiscardNonMatching => {
- Some("All results that does not match any of the rules in the optic will be discarded.".to_string())
- }
-
- optics::Token::Rule => Some("A rule specifies how a particular search result should be treated. \
- It consists of a `Matches` block and an optional `Action`. Any search result that matches the `Matches` block \
- will have the `Action` applied to it. The action can either `Boost`, `Downrank` or `Discard` a result. An empty `Action` is \
- equivalent to a `Boost` of 0.".to_string()),
-
- optics::Token::RankingPipeline => Some("The final ranking consists of multiple stages in a pipeline. Each stage receives the
- best scoring webpages from the stage before it, and uses more accurate yet computationally expensive algorithms to rank
- the pages for the next stage.".to_string()),
-
- optics::Token::Stage => Some("The final ranking consists of multiple stages in a pipeline. Each stage has different fields and
- signals available to it.".to_string()),
-
- optics::Token::Matches => Some("`Matches` dictates the set of criteria a search result should match in order to have the action applied to it. \
- A search result must match all the parts of the `Matches` block in order to match the specific rule.".to_string()),
-
- optics::Token::Site => Some("`Site(\"...\")` matches any search result where the pattern defined in `\"...\"` matches the site of the result. \
- Note that when `Site` is used inside `Like` or `Dislike`, the pattern can only contain simple terms (no `*` and `|`). \n\n\
- When the site is used in a `Matches` block, you can use `*` as a wildcard term and `|` to indicate either the end or start of a string. \n\
- Consider the pattern `\"|sub.*.com|\"`. This will ensure that the terms `sub` and `.` must appear at the beggining of the site, then followed by any \
- domain-name that ends in `.` and `com`. Note that `|` can only be used in the beggining or end of a pattern and the pattern will only match full terms (no substring matching). \n\n\
- This example illustrates the difference between `Domain`, `Site` and `Url`:\n\
- Assume a search result has the url `https://sub.example.org/page`. the domain here is `example.org`, the site is `sub.example.org` and the url is the entire url (with protocol).\
- ".to_string()),
-
- optics::Token::Url => Some("`Url(\"...\")` matches any search result where the pattern defined in `\"...\"` matches the url of the result. \
- You can use `*` as a wildcard term and `|` to indicate either the end or start of a url. \n\
- Consider the pattern `\"https://sub.*.com|\"`. This will ensure that the terms `https`, `:`, `/`, `/`, `sub` and `.` must appear before any term that ends with `.` and `com` \
- in the url. Note that `|` can only be used in the beggining or end of a pattern and the pattern will only match full terms (no substring matching). \n\n\
- This example illustrates the difference between `Domain`, `Site` and `Url`:\n\
- Assume a search result has the url `https://sub.example.org/page`. the domain here is `example.org`, the site is `sub.example.org` and the url is the entire url (with protocol).\
- ".to_string()),
-
- optics::Token::Domain => Some("`Domain(\"...\")` matches any search result where the pattern defined in `\"...\"` matches the domain of the result. \
- You can use `*` as a wildcard term and `|` to indicate either the end or start of a domain. \n\
- Consider the pattern `\"example.org\"`. This is equivalent to doing a phrase search for `\"example.org\"` in the domain. Note that the pattern will only match full terms (no substring matching). \n\n\
- This example illustrates the difference between `Domain`, `Site` and `Url`:\n\
- Assume a search result has the url `https://sub.example.org/page`. the domain here is `example.org`, the site is `sub.example.org` and the url is the entire url (with protocol).\
- ".to_string()),
-
- optics::Token::Title => Some("`Title(\"...\")` matches any search result where the pattern defined in `\"...\"` matches the title of the web page. \
- You can use `*` as a wildcard term and `|` to indicate either the end or start of a title. \n\
- Consider the pattern `\"|Best * ever\"`. This will match any result where the title starts with `Best` followed by any term(s) and then followed by the term `ever`. \
- Note that the pattern will only match full terms (no substring matching) and the modifier `|` can only be used at the end or beggining of the pattern.".to_string()),
-
- optics::Token::Description => Some("`Description(\"...\")` matches any search result where the pattern defined in `\"...\"` matches the description of the web page. \
- You can use `*` as a wildcard term and `|` to indicate either the end or start of a description. \n\
- Consider the pattern `\"|Best * ever\"`. This will match any result where the description starts with `Best` followed by any term(s) and then followed by the term `ever`. \
- Note that the pattern will only match full terms (no substring matching) and the modifier `|` can only be used at the end or beggining of the pattern.".to_string()),
-
- optics::Token::Content => Some("`Content(\"...\")` matches any search result where the pattern defined in `\"...\"` matches the content of the web page. \
- The content of a webpage is all the text that is not part of navigational menues, footers etc. \n\
- You can use `*` as a wildcard term and `|` to indicate either the end or start of the content. \n\
- Consider the pattern `\"Best * ever\"`. This will match any result where the description starts with `Best` followed by any term(s) and then followed by the term `ever`. \
- Note that the pattern will only match full terms (no substring matching) and the modifier `|` can only be used at the end or beggining of the pattern.".to_string()),
-
- optics::Token::MicroformatTag => Some("`MicroformatTag(\"...\")` matches any search result that contains the microformat tag defined in `\"...\"`. \
- This is useful when looking for indieweb pages.".to_string()),
-
- optics::Token::Schema => Some("`Schema(\"...\")` matches any search result that contains the https://schema.org entity defined in `\"...\"`. \
- As an example, `Schema(\"BlogPosting\")` matches all pages that contains the https://schema.org/BlogPosting entity. Note that `Schema` \
- does not support the pattern syntax, but only simple strings.".to_string()),
-
- optics::Token::Ranking => Some("When results are ranked we take a weighted sum of various signals to give each webpage a score for the specific query. \
- The top scored results are then presented to the user. `Ranking` allows you to alter the weight of all the `Signal`s and text `Field`s.".to_string()),
-
- optics::Token::Signal => Some("During ranking of the search results, a number of signals are combined in a weighted sum to create the final score for each search result. \
- `Signal` allows you to change the coefficient used for each signal, and thereby alter the search result ranking. Some supported signals are e.g. \"host_centrality\", \"bm25\" and \"tracker_score\". \
- A complete list of the available signals can be found in the code (https://github.com/StractOrg/Stract/blob/main/src/ranking/signal.rs)".to_string()),
-
- optics::Token::Field => Some("`Field` lets you change how the various text fields are prioritized during ranking (e.g. a search result matching text in the title is probably more relevant than a result where only the body matches). \
- Some supported fields are e.g. \"title\", \"body\", \"backlink_text\" and \"site\". \
- A complete list of available fields can be seen in the code (https://github.com/StractOrg/Stract/blob/main/src/schema.rs)".to_string()),
-
- optics::Token::Action => Some("`Action` defines which action should be applied to the matching search result. The result can either be boosted, downranked or discarded.".to_string()),
-
- optics::Token::Boost => Some("`Boost(...)` boosts the search result by the number specified in `...`.".to_string()),
-
- optics::Token::Downrank => Some("`Downrank(...)` downranks the search result by the number specified in `...`. A higher number further downranks the search result.".to_string()),
-
- optics::Token::Discard => Some("`Discard` discards the matching search result completely from the results page.".to_string()),
-
- optics::Token::Like => Some("`Like(Site(...))` lets you like specific hosts. During ranking, we will calculate a centrality meassure from all you liked sites \
- so results that are heavily linked to from your liked sites will be favored. Note therefore, that `Like` not only alters the ranking of the specifc site, \
- but also sites that are heavily linked to from the liked site.".to_string()),
-
- optics::Token::Dislike => Some("`Dislike(Site(...))` lets you dislike specifc hosts. During ranking, we will calculate a centrality meassure from all you dislike sites \
- so results that are heavily linked to from your disliked sites will be downranked. Note therefore, that `Dislike` not only alters the ranking of the specifc site, \
- but also sites that are heavily linked to from the disliked site.".to_string()),
-
-
- _ => None,
- };
-
+ let msg = docs::token_docs(&token);
msg.map(|msg| Hover {
- contents: HoverContents::Scalar(MarkedString::String(msg)),
+ contents: HoverContents::Array(vec![
+ MarkedString::LanguageString(LanguageString {
+ language: "optic".to_string(),
+ value: token.to_string(),
+ }),
+ MarkedString::String(msg.to_string()),
+ ]),
range: Some(Range {
start: offset_to_pos(start, &file.source),
end: offset_to_pos(end, &file.source),
@@ -288,29 +201,23 @@ impl OpticsBackend {
fn err_to_diagnostic(err: optics::Error, source: &str) -> Diagnostic {
match err {
optics::Error::UnexpectedEof { expected } => {
- let mut message = String::new();
-
- message.push_str("Unexpected EOF.\n");
- message.push_str("Expected one of the following tokens:");
-
- for ex in expected {
- message.push('\n');
- message.push_str(" - ");
- message.push_str(ex.as_str());
- }
+ let message = [
+ "Unexpected EOF.".to_string(),
+ "Expected one of the following tokens:".to_string(),
+ ]
+ .into_iter()
+ .chain(expected.iter().map(|ex| format!(" - {ex}")))
+ .join("\n");
+
+ let eof_position = Position {
+ line: source.lines().count() as u32,
+ character: source.lines().last().unwrap_or_default().len() as u32,
+ };
Diagnostic {
range: Range {
- start: Position {
- line: source.lines().count() as u32,
- character: source.lines().last().map(|l| l.len()).unwrap_or_default()
- as u32,
- },
- end: Position {
- line: source.lines().count() as u32,
- character: source.lines().last().map(|l| l.len()).unwrap_or_default()
- as u32,
- },
+ start: eof_position,
+ end: eof_position,
},
severity: Some(DiagnosticSeverity::ERROR),
message,
@@ -321,15 +228,13 @@ fn err_to_diagnostic(err: optics::Error, source: &str) -> Diagnostic {
token: (start, tok, end),
expected,
} => {
- let mut message = String::new();
- message.push_str(&format!("Unexpected token \"{tok}\"\n"));
- message.push_str("Expected one of the following tokens:");
-
- for ex in expected {
- message.push('\n');
- message.push_str(" - ");
- message.push_str(ex.as_str());
- }
+ let message = [
+ format!("Unexpected token \"{tok}\""),
+ "Expected one of the following tokens:".to_string(),
+ ]
+ .into_iter()
+ .chain(expected.iter().map(|ex| format!(" - {ex}")))
+ .join("\n");
Diagnostic {
range: Range {
@@ -409,7 +314,7 @@ fn offset_to_pos(offset: usize, src: &str) -> Position {
Position::new(l as _, 0)
} else {
let l = src[..offset].lines().count() - 1;
- let c = src[..offset].lines().last().unwrap().len();
+ let c = src[..offset].lines().last().unwrap_or_default().len();
Position::new(l as _, c as _)
}
}