Skip to content

Commit

Permalink
Merge pull request #147 from etaoins/make-source-type-generic
Browse files Browse the repository at this point in the history
Make source type generic
  • Loading branch information
brendanzab authored Jan 5, 2020
2 parents c760bff + 6000d43 commit 92229c1
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 63 deletions.
28 changes: 16 additions & 12 deletions codespan-lsp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ fn location_to_position(
}
}

pub fn byte_index_to_position(
files: &Files,
pub fn byte_index_to_position<Source: AsRef<str>>(
files: &Files<Source>,
file_id: FileId,
byte_index: ByteIndex,
) -> Result<lsp::Position, Error> {
Expand All @@ -103,7 +103,11 @@ pub fn byte_index_to_position(
location_to_position(line_str, location.line, column, byte_index)
}

pub fn byte_span_to_range(files: &Files, file_id: FileId, span: Span) -> Result<lsp::Range, Error> {
pub fn byte_span_to_range<Source: AsRef<str>>(
files: &Files<Source>,
file_id: FileId,
span: Span,
) -> Result<lsp::Range, Error> {
Ok(lsp::Range {
start: byte_index_to_position(files, file_id, span.start())?,
end: byte_index_to_position(files, file_id, span.end())?,
Expand Down Expand Up @@ -137,8 +141,8 @@ pub fn character_to_line_offset(line: &str, character: u64) -> Result<ByteOffset
}
}

pub fn position_to_byte_index(
files: &Files,
pub fn position_to_byte_index<Source: AsRef<str>>(
files: &Files<Source>,
file_id: FileId,
position: &lsp::Position,
) -> Result<ByteIndex, Error> {
Expand All @@ -149,8 +153,8 @@ pub fn position_to_byte_index(
Ok(line_span.start() + byte_offset)
}

pub fn range_to_byte_span(
files: &Files,
pub fn range_to_byte_span<Source: AsRef<str>>(
files: &Files<Source>,
file_id: FileId,
range: &lsp::Range,
) -> Result<Span, Error> {
Expand All @@ -176,8 +180,8 @@ pub fn make_lsp_severity(severity: Severity) -> lsp::DiagnosticSeverity {
/// `correlate_file_url` is necessary to resolve codespan `FileName`s
///
/// `code` and `file` are left empty by this function
pub fn make_lsp_diagnostic(
files: &Files,
pub fn make_lsp_diagnostic<Source: AsRef<str>>(
files: &Files<Source>,
source: impl Into<Option<String>>,
diagnostic: Diagnostic,
mut correlate_file_url: impl FnMut(FileId) -> Result<Url, ()>,
Expand Down Expand Up @@ -256,7 +260,7 @@ let test = 2
let test1 = ""
test
"#;
let mut files = Files::new();
let mut files = Files::<&'static str>::new();
let file_id = files.add("test", text);
let pos = position_to_byte_index(
&files,
Expand All @@ -276,7 +280,7 @@ test

#[test]
fn unicode_get_byte_index() {
let mut files = Files::new();
let mut files = Files::<&'static str>::new();
let file_id = files.add("unicode", UNICODE);

let result = position_to_byte_index(
Expand All @@ -302,7 +306,7 @@ test

#[test]
fn unicode_get_position() {
let mut files = Files::new();
let mut files = Files::<&'static str>::new();
let file_id = files.add("unicode", UNICODE);

let result = byte_index_to_position(&files, file_id, ByteIndex::from(5));
Expand Down
2 changes: 1 addition & 1 deletion codespan-reporting/examples/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct Opts {

fn main() {
let opts = Opts::from_args();
let mut files = Files::new();
let mut files = Files::<String>::new();

let file_id1 = files.add(
"Data/Nat.fun",
Expand Down
7 changes: 4 additions & 3 deletions codespan-reporting/src/term/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ pub use termcolor;
pub use self::config::{Chars, Config, DisplayStyle, Styles};

/// Emit a diagnostic using the given writer, context, config, and files.
pub fn emit(
pub fn emit<Source: AsRef<str>>(
writer: &mut (impl WriteColor + ?Sized),
config: &Config,
files: &Files,
files: &Files<Source>,
diagnostic: &Diagnostic,
) -> io::Result<()> {
use self::views::{RichDiagnostic, ShortDiagnostic};
Expand Down Expand Up @@ -97,7 +97,8 @@ mod tests {

#[test]
fn unsized_emit() {
let mut files = Files::new();
let mut files = Files::<&'static str>::new();

let id = files.add("test", "");
emit(
&mut termcolor::NoColor::new(Vec::<u8>::new()) as &mut dyn WriteColor,
Expand Down
31 changes: 23 additions & 8 deletions codespan-reporting/src/term/views/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ use crate::term::Config;
use super::{Header, Locus, NewLine, SourceSnippet};

/// Output a richly formatted diagnostic, with source code previews.
pub struct RichDiagnostic<'a> {
files: &'a Files,
pub struct RichDiagnostic<'a, Source>
where
Source: AsRef<str>,
{
files: &'a Files<Source>,
diagnostic: &'a Diagnostic,
}

impl<'a> RichDiagnostic<'a> {
pub fn new(files: &'a Files, diagnostic: &'a Diagnostic) -> RichDiagnostic<'a> {
impl<'a, Source> RichDiagnostic<'a, Source>
where
Source: AsRef<str>,
{
pub fn new(files: &'a Files<Source>, diagnostic: &'a Diagnostic) -> Self {
RichDiagnostic { files, diagnostic }
}

Expand Down Expand Up @@ -63,13 +69,22 @@ impl<'a> RichDiagnostic<'a> {
}

/// Output a short diagnostic, with a line number, severity, and message.
pub struct ShortDiagnostic<'a> {
files: &'a Files,
pub struct ShortDiagnostic<'a, Source>
where
Source: AsRef<str>,
{
files: &'a Files<Source>,
diagnostic: &'a Diagnostic,
}

impl<'a> ShortDiagnostic<'a> {
pub fn new(files: &'a Files, diagnostic: &'a Diagnostic) -> ShortDiagnostic<'a> {
impl<'a, Source> ShortDiagnostic<'a, Source>
where
Source: AsRef<str>,
{
pub fn new(
files: &'a Files<Source>,
diagnostic: &'a Diagnostic,
) -> ShortDiagnostic<'a, Source> {
ShortDiagnostic { files, diagnostic }
}

Expand Down
16 changes: 11 additions & 5 deletions codespan-reporting/src/term/views/source_snippet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,26 @@ pub use self::underline::MarkStyle;
/// = expected type `Int`
/// found type `String`
/// ```
pub struct SourceSnippet<'a> {
files: &'a Files,
pub struct SourceSnippet<'a, Source>
where
Source: AsRef<str>,
{
files: &'a Files<Source>,
file_id: FileId,
spans: Vec<(&'a Label, MarkStyle)>,
notes: &'a [String],
}

impl<'a> SourceSnippet<'a> {
impl<'a, Source> SourceSnippet<'a, Source>
where
Source: AsRef<str>,
{
pub fn new(
files: &'a Files,
files: &'a Files<Source>,
file_id: FileId,
spans: Vec<(&'a Label, MarkStyle)>,
notes: &'a [String],
) -> SourceSnippet<'a> {
) -> Self {
SourceSnippet {
files,
file_id,
Expand Down
2 changes: 1 addition & 1 deletion codespan-reporting/tests/support/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod color_buffer;
use self::color_buffer::ColorBuffer;

pub struct TestData {
pub files: Files,
pub files: Files<String>,
pub diagnostics: Vec<Diagnostic>,
}

Expand Down
Loading

0 comments on commit 92229c1

Please sign in to comment.