Skip to content

Commit

Permalink
feat: add use auto import using rust-analyzer #3
Browse files Browse the repository at this point in the history
  • Loading branch information
patriciobcs committed Nov 11, 2022
1 parent cda4171 commit c208756
Show file tree
Hide file tree
Showing 19 changed files with 463 additions and 185 deletions.
4 changes: 0 additions & 4 deletions build.sh

This file was deleted.

35 changes: 27 additions & 8 deletions compiler/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use json::{object, JsonValue};
use std::collections::HashMap;
use std::fs::{read_dir, File};
use std::io::{self, BufRead, Write};
use std::fs::{read_dir, File, read_to_string};
use std::io::{self, BufRead};
use std::path::Path;

fn parse_config(contents: String, config_name: String) -> String {
Expand Down Expand Up @@ -40,16 +40,22 @@ fn parse_body_line(contents: String, inputs: &mut HashMap<String, usize>) -> Str
}
parsed_contents
}
fn parse_requires_line(contents: String) -> String {
contents.trim().replacen("use ", "", 1).replacen(";", "", 1)
}

fn generate_snippet(filepath: String) -> (String, JsonValue) {
let mut inputs = HashMap::new();
let mut config = object! {
description: "",
prefix: [],
body: [],
requires: [],
description: "",
scope: "expr",
};
let mut title: String = "".into();
let mut is_reading_snippet = false;
let mut is_reading_requires = false;

if let Ok(lines) = read_lines(filepath) {
for line in lines {
Expand All @@ -60,12 +66,18 @@ fn generate_snippet(filepath: String) -> (String, JsonValue) {
config["description"] = parse_config(ip, "description".into()).into();
} else if ip.contains("fn") {
config["prefix"].push(parse_command(ip)).ok();
} else if ip.contains("snippet-start") {
} else if ip.contains("snippet-body-start") {
is_reading_snippet = true;
} else if ip.contains("snippet-end") {
} else if ip.contains("snippet-body-end") {
is_reading_snippet = false;
} else if is_reading_snippet {
config["body"].push(parse_body_line(ip, &mut inputs)).ok();
} else if ip.contains("snippet-requires-start") {
is_reading_requires = true;
} else if ip.contains("snippet-requires-end") {
is_reading_requires = false;
} else if is_reading_requires && ip.contains("use ") {
config["requires"].push(parse_requires_line(ip)).ok();
}
}
}
Expand All @@ -82,7 +94,8 @@ where
}

const SNIPPETS_DIR: &str = "../snippets/src/core";
const SNIPPETS_CONFIG_DIR: &str = "../snippets.json";

const EXTENSION_CONFIG_DIR: &str = "../package.json";

fn main() -> std::io::Result<()> {
let mut snippets = object! {};
Expand All @@ -99,9 +112,15 @@ fn main() -> std::io::Result<()> {
snippets[title] = config;
}

let mut snippets_file = File::create(SNIPPETS_CONFIG_DIR)?;
let extension_config_contents = read_to_string(EXTENSION_CONFIG_DIR).unwrap();

let mut extension_config = json::parse(&extension_config_contents).unwrap();

extension_config["contributes"]["configurationDefaults"]["rust-analyzer.completion.snippets.custom"] = snippets;

snippets_file.write(snippets.dump().as_bytes()).ok();
let mut extension_config_file = File::create(EXTENSION_CONFIG_DIR)?;

extension_config.write_pretty(&mut extension_config_file, 2).ok();

Ok(())
}

0 comments on commit c208756

Please sign in to comment.