move skill code to own file, change caching names/concept
This commit is contained in:
parent
f1a9cee6c3
commit
3eb83f9f4f
67
src/cache.rs
67
src/cache.rs
|
@ -1,5 +1,8 @@
|
|||
use dashmap::DashMap;
|
||||
use std::path::{Path, PathBuf};
|
||||
use pest::error::Error;
|
||||
use tower_lsp::lsp_types::CompletionItem;
|
||||
|
||||
use crate::{parse_global_symbols, parse_skill};
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
struct Position {
|
||||
|
@ -13,61 +16,45 @@ struct Range {
|
|||
to: Position,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
struct CachedScope {
|
||||
range: Range,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Debug, Clone)]
|
||||
pub struct CachedItems {
|
||||
global_tokens: Vec<String>,
|
||||
scopes: Vec<CachedScope>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct DocumentCache {
|
||||
pub documents: DashMap<PathBuf, CachedItems>,
|
||||
pub struct SymbolCache {
|
||||
pub symbols: DashMap<String, Vec<CompletionItem>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct FileNotInCache;
|
||||
|
||||
impl DocumentCache {
|
||||
pub fn new() -> DocumentCache {
|
||||
DocumentCache {
|
||||
documents: DashMap::new(),
|
||||
impl SymbolCache {
|
||||
pub fn new() -> SymbolCache {
|
||||
SymbolCache {
|
||||
symbols: DashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_document(&self, path: PathBuf, items: CachedItems) {
|
||||
self.documents.insert(path, items);
|
||||
pub fn update(&self, path: &str) -> Vec<CompletionItem> {
|
||||
let parsed = parse_skill(path);
|
||||
for rule in parsed {
|
||||
match parse_global_symbols(rule) {
|
||||
Ok(_) => {}
|
||||
Err(_) => {}
|
||||
}
|
||||
}
|
||||
return self.symbols.get(path).unwrap().to_vec();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::cache::{CachedItems, DocumentCache};
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
use crate::cache::SymbolCache;
|
||||
// use std::collections::HashMap;
|
||||
// use std::path::Path;
|
||||
|
||||
#[test]
|
||||
fn insert() {
|
||||
let mut d = DocumentCache::new(Path::new("/example/workdir").to_path_buf());
|
||||
d.update_document(
|
||||
Path::new("example_file.ext").to_path_buf(),
|
||||
CachedItems {
|
||||
global_tokens: vec![],
|
||||
scopes: vec![],
|
||||
},
|
||||
);
|
||||
let mut comp = HashMap::new();
|
||||
comp.insert(
|
||||
Path::new("example_file.ext").to_path_buf(),
|
||||
CachedItems {
|
||||
global_tokens: vec![],
|
||||
scopes: vec![],
|
||||
},
|
||||
);
|
||||
assert_eq!(d.documents, comp)
|
||||
let mut d = SymbolCache::new();
|
||||
d.update();
|
||||
// let mut comp = HashMap::new();
|
||||
// comp.insert();
|
||||
// assert_eq!(d.documents, comp)
|
||||
}
|
||||
}
|
||||
|
|
46
src/main.rs
46
src/main.rs
|
@ -1,17 +1,16 @@
|
|||
mod cache;
|
||||
use cache::DocumentCache;
|
||||
use cache::SymbolCache;
|
||||
|
||||
mod skill;
|
||||
use skill::{parse_global_symbols, parse_skill};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use tower_lsp::jsonrpc::{Error, ErrorCode, Result};
|
||||
use tower_lsp::lsp_types::notification::Notification;
|
||||
use tower_lsp::lsp_types::*;
|
||||
use tower_lsp::{Client, LanguageServer, LspService, Server};
|
||||
|
||||
use crate::pest::Parser;
|
||||
|
||||
extern crate glob;
|
||||
extern crate pest;
|
||||
#[macro_use]
|
||||
|
@ -20,13 +19,9 @@ extern crate pest_derive;
|
|||
#[derive(Debug)]
|
||||
struct Backend {
|
||||
client: Client,
|
||||
cache: DocumentCache,
|
||||
cache: SymbolCache,
|
||||
}
|
||||
|
||||
#[derive(Parser)]
|
||||
#[grammar = "./skill.pest"]
|
||||
struct SkillParser;
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
struct CustomNotificationParams {
|
||||
title: String,
|
||||
|
@ -67,9 +62,11 @@ impl LanguageServer for Backend {
|
|||
self.client
|
||||
.log_message(MessageType::INFO, format!("pattern used: {:?}", pattern))
|
||||
.await;
|
||||
|
||||
for entry in glob::glob(pattern.as_str()).expect("no file to cache in root_dir") {
|
||||
match entry {
|
||||
Ok(path) => {
|
||||
self.cache.update(path.to_str().unwrap());
|
||||
self.client
|
||||
.log_message(MessageType::INFO, format!("caching {:?}", path.display()))
|
||||
.await
|
||||
|
@ -132,27 +129,12 @@ impl LanguageServer for Backend {
|
|||
}
|
||||
|
||||
async fn completion(&self, cparams: CompletionParams) -> Result<Option<CompletionResponse>> {
|
||||
let doc = cparams.text_document_position.text_document.uri.path();
|
||||
// let line = cparams.text_document_position.position.line;
|
||||
// let character = cparams.text_document_position.position.character;
|
||||
let content = fs::read_to_string(doc).expect("could not read");
|
||||
let file = SkillParser::parse(Rule::skill, &content)
|
||||
.expect("unsuccessful parse")
|
||||
.next()
|
||||
.unwrap();
|
||||
|
||||
let mut symbols: Vec<CompletionItem> = vec![];
|
||||
|
||||
for record in file.into_inner() {
|
||||
match record.as_rule() {
|
||||
Rule::assign => symbols.push(CompletionItem {
|
||||
label: record.into_inner().next().unwrap().as_str().to_string(),
|
||||
kind: Some(CompletionItemKind::VARIABLE),
|
||||
..Default::default()
|
||||
}),
|
||||
_ => {}
|
||||
};
|
||||
}
|
||||
let symbols: Vec<CompletionItem> = self
|
||||
.cache
|
||||
.symbols
|
||||
.get_mut(&cparams.text_document_position.text_document.uri.to_string())
|
||||
.unwrap()
|
||||
.to_vec();
|
||||
|
||||
Ok(Some(CompletionResponse::Array(symbols)))
|
||||
}
|
||||
|
@ -166,7 +148,7 @@ async fn main() {
|
|||
|
||||
let (service, socket) = LspService::new(|client| Backend {
|
||||
client,
|
||||
cache: DocumentCache::new(),
|
||||
cache: SymbolCache::new(),
|
||||
});
|
||||
Server::new(stdin, stdout, socket).serve(service).await;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
use pest::error::Error;
|
||||
use pest::iterators::Pair;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[grammar = "./skill.pest"]
|
||||
pub struct SkillParser;
|
||||
|
||||
pub fn parse_skill(path: &str) -> Vec<Pair<Rule>> {
|
||||
vec![]
|
||||
}
|
||||
pub fn parse_global_symbols(token: Pair<Rule>) -> Result<&str, Error<Rule>> {
|
||||
Ok("")
|
||||
}
|
Loading…
Reference in New Issue