1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
//! Utilities for translating from codespan types into Language Server Protocol (LSP) types

extern crate codespan;
extern crate codespan_reporting;

extern crate failure;
#[macro_use]
extern crate failure_derive;

extern crate languageserver_types;
extern crate url;

use codespan::{
    ByteIndex, ByteIndexError, ByteOffset, CodeMap, ColumnIndex, FileMap, FileName, LineIndex,
    LineIndexError, LocationError, RawIndex, RawOffset, Span,
};
use codespan_reporting::{Diagnostic, Severity};
use languageserver_types as lsp;
use url::Url;

#[derive(Debug, Fail, PartialEq)]
pub enum Error {
    #[fail(display = "Position is outside of codemap {}", _0)]
    SpanOutsideCodeMap(ByteIndex),
    #[fail(display = "Unable to correlate filename `{}` to url", _0)]
    UnableToCorrelateFilename(FileName),
    #[fail(display = "{}", _0)]
    ByteIndexError(#[cause] ByteIndexError),
    #[fail(display = "{}", _0)]
    LocationError(#[cause] LocationError),
    #[fail(display = "{}", _0)]
    LineIndexError(#[cause] LineIndexError),
}

impl From<ByteIndexError> for Error {
    fn from(e: ByteIndexError) -> Error {
        Error::ByteIndexError(e)
    }
}

impl From<LocationError> for Error {
    fn from(e: LocationError) -> Error {
        Error::LocationError(e)
    }
}

impl From<LineIndexError> for Error {
    fn from(e: LineIndexError) -> Error {
        Error::LineIndexError(e)
    }
}

fn location_to_position(
    line_str: &str,
    line: LineIndex,
    column: ColumnIndex,
    byte_index: ByteIndex,
) -> Result<lsp::Position, Error> {
    if column.to_usize() > line_str.len() {
        let max = ColumnIndex(line_str.len() as RawIndex);
        let given = column;

        Err(LocationError::ColumnOutOfBounds { given, max }.into())
    } else if !line_str.is_char_boundary(column.to_usize()) {
        let given = byte_index;

        Err(ByteIndexError::InvalidCharBoundary { given }.into())
    } else {
        let line_utf16 = line_str[..column.to_usize()].encode_utf16();
        let character = line_utf16.count() as u64;
        let line = line.to_usize() as u64;

        Ok(lsp::Position { line, character })
    }
}

pub fn byte_index_to_position<S>(
    source: &FileMap<S>,
    pos: ByteIndex,
) -> Result<lsp::Position, Error>
where
    S: AsRef<str>,
{
    let line = source.find_line(pos)?;
    let line_span = source.line_span(line).unwrap();
    let line_str = source.src_slice(line_span).unwrap();
    let column = ColumnIndex::from((pos - line_span.start()).0 as RawIndex);

    location_to_position(line_str, line, column, pos)
}

pub fn byte_span_to_range<S>(
    source: &FileMap<S>,
    span: Span<ByteIndex>,
) -> Result<lsp::Range, Error>
where
    S: AsRef<str>,
{
    Ok(lsp::Range {
        start: byte_index_to_position(source, span.start())?,
        end: byte_index_to_position(source, span.end())?,
    })
}

pub fn character_to_line_offset(line: &str, character: u64) -> Result<ByteOffset, Error> {
    let line_len = ByteOffset::from(line.len() as RawOffset);
    let mut character_offset = 0;

    let mut chars = line.chars();
    while let Some(ch) = chars.next() {
        if character_offset == character {
            let chars_off = ByteOffset::from_str(chars.as_str());
            let ch_off = ByteOffset::from_char_utf8(ch);

            return Ok(line_len - chars_off - ch_off);
        }

        character_offset += ch.len_utf16() as u64;
    }

    // Handle positions after the last character on the line
    if character_offset == character {
        Ok(line_len)
    } else {
        Err(LocationError::ColumnOutOfBounds {
            given: ColumnIndex(character_offset as RawIndex),
            max: ColumnIndex(line.len() as RawIndex),
        }.into())
    }
}

pub fn position_to_byte_index<S>(
    source: &FileMap<S>,
    position: &lsp::Position,
) -> Result<ByteIndex, Error>
where
    S: AsRef<str>,
{
    let line_span = source.line_span(LineIndex::from(position.line as RawIndex))?;
    let src_slice = source.src_slice(line_span).unwrap();
    let byte_offset = character_to_line_offset(src_slice, position.character)?;

    Ok(line_span.start() + byte_offset)
}

pub fn range_to_byte_span<S>(
    source: &FileMap<S>,
    range: &lsp::Range,
) -> Result<Span<ByteIndex>, Error>
where
    S: AsRef<str>,
{
    Ok(Span::new(
        position_to_byte_index(source, &range.start)?,
        position_to_byte_index(source, &range.end)?,
    ))
}

pub fn make_lsp_severity(severity: Severity) -> lsp::DiagnosticSeverity {
    match severity {
        Severity::Error | Severity::Bug => lsp::DiagnosticSeverity::Error,
        Severity::Warning => lsp::DiagnosticSeverity::Warning,
        Severity::Note => lsp::DiagnosticSeverity::Information,
        Severity::Help => lsp::DiagnosticSeverity::Hint,
    }
}

const UNKNOWN_POS: lsp::Position = lsp::Position {
    character: 0,
    line: 0,
};

const UNKNOWN_RANGE: lsp::Range = lsp::Range {
    start: UNKNOWN_POS,
    end: UNKNOWN_POS,
};

/// Translates a `codespan_reporting::Diagnostic` to a `languageserver_types::Diagnostic`.
///
/// Since the language client requires `Url`s to locate the errors `codespan_name_to_file` is
/// necessary to resolve codespan `FileName`s
///
/// `code` and `source` are left empty by this function
pub fn make_lsp_diagnostic<F>(
    code_map: &CodeMap,
    diagnostic: Diagnostic,
    mut codespan_name_to_file: F,
) -> Result<lsp::Diagnostic, Error>
where
    F: FnMut(&FileName) -> Result<Url, ()>,
{
    use codespan_reporting::LabelStyle;

    let find_file = |index| {
        code_map
            .find_file(index)
            .ok_or_else(|| Error::SpanOutsideCodeMap(index))
    };

    // We need a position for the primary error so take the span from the first primary label
    let (primary_file_map, primary_label_range) = {
        let first_primary_label = diagnostic
            .labels
            .iter()
            .find(|label| label.style == LabelStyle::Primary);

        match first_primary_label {
            Some(label) => {
                let file_map = find_file(label.span.start())?;
                (Some(file_map), byte_span_to_range(&file_map, label.span)?)
            },
            None => (None, UNKNOWN_RANGE),
        }
    };

    let related_information = diagnostic
        .labels
        .into_iter()
        .map(|label| {
            let (file_map, range) = match primary_file_map {
                // If the label's span does not point anywhere, assume it comes from the same file
                // as the primary label
                Some(file_map) if label.span.start() == ByteIndex::none() => {
                    (file_map, UNKNOWN_RANGE)
                },
                Some(_) | None => {
                    let file_map = find_file(label.span.start())?;
                    let range = byte_span_to_range(file_map, label.span)?;

                    (file_map, range)
                },
            };

            let uri = codespan_name_to_file(file_map.name())
                .map_err(|()| Error::UnableToCorrelateFilename(file_map.name().clone()))?;

            Ok(lsp::DiagnosticRelatedInformation {
                location: lsp::Location { uri, range },
                message: label.message.unwrap_or(String::new()),
            })
        })
        .collect::<Result<Vec<_>, Error>>()?;

    Ok(lsp::Diagnostic {
        message: diagnostic.message,
        range: primary_label_range,
        severity: Some(make_lsp_severity(diagnostic.severity)),
        related_information: if related_information.is_empty() {
            None
        } else {
            Some(related_information)
        },
        ..lsp::Diagnostic::default()
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn position() {
        let text = r#"
let test = 2
let test1 = ""
te
"#;
        let source = FileMap::new("".into(), text);
        let pos = position_to_byte_index(
            &source,
            &lsp::Position {
                line: 3,
                character: 2,
            },
        ).unwrap();
        assert_eq!((3.into(), 2.into()), source.location(pos).unwrap());
    }

    // The protocol specifies that each `character` in position is a UTF-16 character.
    // This means that `å` and `ä` here counts as 1 while `𐐀` counts as 2.
    const UNICODE: &str = "åä t𐐀b";

    #[test]
    fn unicode_get_byte_index() {
        let source = FileMap::new("".into(), UNICODE);

        let result = position_to_byte_index(
            &source,
            &lsp::Position {
                line: 0,
                character: 3,
            },
        );
        assert_eq!(result, Ok(ByteIndex::from(6)));

        let result = position_to_byte_index(
            &source,
            &lsp::Position {
                line: 0,
                character: 6,
            },
        );
        assert_eq!(result, Ok(ByteIndex::from(11)));
    }

    #[test]
    fn unicode_get_position() {
        let source = FileMap::new("".into(), UNICODE);

        let result = byte_index_to_position(&source, ByteIndex::from(6));
        assert_eq!(
            result,
            Ok(lsp::Position {
                line: 0,
                character: 3,
            })
        );

        let result = byte_index_to_position(&source, ByteIndex::from(11));
        assert_eq!(
            result,
            Ok(lsp::Position {
                line: 0,
                character: 6,
            })
        );
    }
}