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

Commit

Permalink
Auto merge of #1326 - Xanewok:alexreg-cosmetic-2, r=Xanewok
Browse files Browse the repository at this point in the history
Various cosmetic improvements

Closes #1314 (same PR + follow up test fixes).
  • Loading branch information
bors committed Feb 24, 2019
2 parents b5a02e0 + e384730 commit 17a4394
Show file tree
Hide file tree
Showing 110 changed files with 576 additions and 951 deletions.
41 changes: 16 additions & 25 deletions rls/src/actions/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Conversion of raw rustc-emitted JSON messages into LSP diagnostics.
//!
//! Data definitions for diagnostics can be found in the Rust compiler for:
//! 1. Internal diagnostics at src/librustc_errors/diagnostic.rs.
//! 2. Emitted JSON format at src/libsyntax/json.rs.
//! 1. Internal diagnostics at `src/librustc_errors/diagnostic.rs`.
//! 2. Emitted JSON format at `src/libsyntax/json.rs`.

use std::collections::HashMap;
use std::iter;
Expand Down Expand Up @@ -98,7 +88,8 @@ pub fn parse_diagnostics(

let mut diagnostics = HashMap::new();

// If the client doesn't support related information, emit separate diagnostics for secondary spans
// If the client doesn't support related information, emit separate diagnostics for
// secondary spans.
let diagnostic_spans = if related_information_support { &primaries } else { &message.spans };

for (path, diagnostic) in diagnostic_spans.iter().map(|span| {
Expand Down Expand Up @@ -131,8 +122,8 @@ pub fn parse_diagnostics(

let rls_span = {
let mut span = span;
// if span points to a macro, search through the expansions
// for a more useful source location
// If span points to a macro, search through the expansions
// for a more useful source location.
while span.file_name.ends_with(" macros>") && span.expansion.is_some() {
span = &span.expansion.as_ref().unwrap().span;
}
Expand Down Expand Up @@ -244,8 +235,8 @@ fn make_suggestions<'a>(
.collect();

// Suggestions are displayed at primary span, so if the change is somewhere
// else, be sure to specify that
// TODO: In theory this can even point to different files - does that happen in practice?
// else, be sure to specify that.
// TODO: In theory this can even point to different files -- does that happen in practice?
for suggestion in &mut suggestions {
if !suggestion.range.is_within(&primary_range) {
let line = suggestion.range.start.line + 1; // as 1-based
Expand Down Expand Up @@ -275,7 +266,7 @@ fn label_suggestion(span: &DiagnosticSpan, label: &str) -> Option<Suggestion> {

trait IsWithin {
/// Returns whether `other` is considered within `self`
/// note: a thing should be 'within' itself
/// NOTE: a thing should be 'within' itself.
fn is_within(&self, other: &Self) -> bool;
}

Expand Down Expand Up @@ -304,9 +295,9 @@ impl IsWithin for Range {
}
}

/// Tests for formatted messages from the compilers json output
/// run cargo with `--message-format=json` to generate the json for new tests and add .json
/// message files to '$FIXTURES_DIR/compiler_message/'
/// Tests for formatted messages from the compiler's JSON output.
/// Runs cargo with `--message-format=json` to generate the JSON for new tests and add JSON
/// message files to the `$FIXTURES_DIR/compiler_message/` directory.
#[cfg(test)]
mod diagnostic_message_test {
use super::*;
Expand All @@ -332,7 +323,7 @@ mod diagnostic_message_test {

pub(super) trait FileDiagnosticTestExt {
fn single_file_results(&self) -> &Vec<(Diagnostic, Vec<Suggestion>)>;
/// Returns (primary message, secondary messages)
/// Returns `(primary message, secondary messages)`.
fn to_messages(&self) -> Vec<(String, Vec<String>)>;
fn to_primary_messages(&self) -> Vec<String>;
fn to_secondary_messages(&self) -> Vec<String>;
Expand Down Expand Up @@ -419,7 +410,7 @@ mod diagnostic_message_test {
assert_eq!(messages[0].1, vec!["consider giving `v` a type", "cannot infer type for `T`"]);

// Check if we don't emit related information if it's not supported and
// if secondary spans are emitted as separate diagnostics
// if secondary spans are emitted as separate diagnostics.
let messages = parse_compiler_message(
&read_fixture("compiler_message/type-annotations-needed.json"),
false,
Expand Down Expand Up @@ -477,7 +468,7 @@ mod diagnostic_message_test {
cannot borrow mutably",
);

// note: consider message becomes a suggestion
// NOTE: 'consider' message becomes a suggestion.
assert_eq!(
messages[0].1,
vec!["consider changing this to `mut string`", "cannot borrow mutably",]
Expand Down Expand Up @@ -675,7 +666,7 @@ help: consider borrowing here: `&string`"#,
}
}

/// Tests for creating suggestions from the compilers json output
/// Tests for creating suggestions from the compilers JSON output.
#[cfg(test)]
mod diagnostic_suggestion_test {
use self::diagnostic_message_test::*;
Expand Down
21 changes: 6 additions & 15 deletions rls/src/actions/format.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Code formatting using Rustfmt - by default using statically-linked one or
//! Code formatting using Rustfmt -- by default using statically-linked one or
//! possibly running Rustfmt binary specified by the user.

use std::env::temp_dir;
Expand All @@ -22,12 +12,12 @@ use rand::{distributions, thread_rng, Rng};
use rustfmt_nightly::{Config, Input, Session};
use serde_json;

/// Specified which `rustfmt` to use.
/// Specifies which `rustfmt` to use.
#[derive(Clone)]
pub enum Rustfmt {
/// (Path to external `rustfmt`, cwd where it should be spawned at)
/// `(path to external `rustfmt`, current working directory to spawn at)`
External(PathBuf, PathBuf),
/// Statically linked `rustfmt`
/// Statically linked `rustfmt`.
Internal,
}

Expand Down Expand Up @@ -90,7 +80,8 @@ fn format_internal(input: String, config: Config) -> Result<String, String> {

match session.format(Input::Text(input)) {
Ok(report) => {
// Session::format returns Ok even if there are any errors, i.e., parsing errors.
// `Session::format` returns `Ok` even if there are any errors, i.e., parsing
// errors.
if session.has_operational_errors() || session.has_parsing_errors() {
debug!("reformat: format_input failed: has errors, report = {}", report);

Expand Down
Loading

0 comments on commit 17a4394

Please sign in to comment.