Skip to content
This repository has been archived by the owner on Dec 17, 2022. It is now read-only.

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Jun 15, 2020
0 parents commit 44a2f6f
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# force lf newlines
* text=auto eol=lf

*.rs text eol=lf
*.ts text eol=lf
*.js text eol=lf
*.json text eol=lf
*.toml text eol=lf
*.md text eol=lf
*.sh text eol=lf
*.yml text eol=lf

*.jpg binary
*.png binary
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
Cargo.lock
17 changes: 17 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "dprint-plugin-rustfmt"
version = "0.1.0"
authors = ["David Sherret <[email protected]>"]
edition = "2018"

[lib]
crate-type = ["lib", "cdylib"]

[dependencies]
dprint-core = "0.22.0"
rustfmt-nightly = "1.4.18"
serde = { version = "1.0.88", features = ["derive"] }
serde_json = "1.0"

[profile.release]
lto = true
23 changes: 23 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
The MIT License (MIT)

Copyright (c) 2020 David Sherret

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.

rustfmt: Copyright (c) 2016 The Rust Project Developers (MIT and Apache 2.0 License)
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# dprint-plugin-rustfmt

[![CI](https://github.com/dprint/dprint-plugin-rustfmt/workflows/CI/badge.svg)](https://github.com/dprint/dprint-plugin-rustfmt/actions?query=workflow%3ACI)

Wrapper around [rustfmt](https://github.com/rust-lang/rustfmt) in order to use it as a dprint plugin.
4 changes: 4 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
export CFG_RELEASE=1.45.0-nightly
export CFG_RELEASE_CHANNEL=nightly
cargo +nightly build --release --target wasm32-unknown-unknown --verbose
4 changes: 4 additions & 0 deletions setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
mkdir librustc_data_structures
cd librustc_data_structures
svn export https://github.com/rust-lang/rust.git/trunk/src/librustc_data_structures
69 changes: 69 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use rustfmt_nightly::{Input, Session, Config};

use std::path::PathBuf;
use std::collections::HashMap;
use serde::{Serialize, Deserialize};
use dprint_core::generate_plugin_code;
use dprint_core::configuration::{GlobalConfiguration, ResolveConfigurationResult, get_unknown_property_diagnostics};

#[derive(Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct Configuration {
// add configuration properties here
}

fn resolve_config(
config: HashMap<String, String>,
global_config: &GlobalConfiguration,
) -> ResolveConfigurationResult<Configuration> {

ResolveConfigurationResult {
diagnostics: Vec::new(),
config: Configuration {}
}
}

fn get_plugin_config_key() -> String {
// return the JSON object key name used in the configuration file
String::from("rustfmt")
}

fn get_plugin_file_extensions() -> Vec<String> {
vec![String::from("rs")]
}

fn get_plugin_help_url() -> String {
String::from("https://dprint.dev/plugins/rustfmt")
}

fn get_plugin_config_schema_url() -> String {
// for now, return an empty string. Return a schema url once VSCode
// supports $schema properties in descendant objects:
// https://github.com/microsoft/vscode/issues/98443
String::new()
}

fn get_plugin_license_text() -> String {
std::str::from_utf8(include_bytes!("../LICENSE")).unwrap().into()
}

fn format_text(
_: &PathBuf,
file_text: &str,
config: &Configuration,
) -> Result<String, String> {
use rustfmt_nightly::{EmitMode, Edition};

let mut out = Vec::with_capacity(file_text.len());
{
let input = Input::Text(String::from(file_text));
let mut config = Config::default();
config.set().emit_mode(EmitMode::Stdout);
config.set().edition(Edition::Edition2018);
let mut session = Session::new(config, Some(&mut out));
session.format(input).unwrap();
}
Ok(String::from_utf8(out).unwrap())
}

generate_plugin_code!();

0 comments on commit 44a2f6f

Please sign in to comment.