mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-25 11:08:51 +01:00
Remove linesModel, use new APIs
This commit is contained in:
@@ -10,7 +10,6 @@ import SchemaService = require('../jsonSchemaService');
|
||||
import JsonSchema = require('../json-toolbox/jsonSchema');
|
||||
import {JSONCompletion} from '../jsonCompletion';
|
||||
import {IXHROptions, IXHRResponse} from '../utils/httpRequest';
|
||||
import {create as createLinesModel} from '../utils/lines';
|
||||
|
||||
import {CompletionItem, CompletionItemKind, CompletionOptions, ITextDocument, TextDocumentIdentifier, TextDocumentPosition, Range, Position, TextEdit} from 'vscode-languageserver';
|
||||
|
||||
@@ -40,9 +39,8 @@ suite('JSON Completion', () => {
|
||||
|
||||
var document = ITextDocument.create(uri, value);
|
||||
var textDocumentLocation = TextDocumentPosition.create(uri, Position.create(0, idx));
|
||||
var lines = createLinesModel(value);
|
||||
var jsonDoc = Parser.parse(value);
|
||||
return completionProvider.doSuggest(document, textDocumentLocation, lines, jsonDoc);
|
||||
return completionProvider.doSuggest(document, textDocumentLocation, jsonDoc);
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -10,7 +10,6 @@ import SchemaService = require('../jsonSchemaService');
|
||||
import JsonSchema = require('../json-toolbox/jsonSchema');
|
||||
import {JSONCompletion} from '../jsonCompletion';
|
||||
import {IXHROptions, IXHRResponse} from '../utils/httpRequest';
|
||||
import {create as createLinesModel} from '../utils/lines';
|
||||
import {JSONDocumentSymbols} from '../jsonDocumentSymbols';
|
||||
|
||||
import {SymbolInformation, SymbolKind, TextDocumentIdentifier, ITextDocument, TextDocumentPosition, Range, Position, TextEdit} from 'vscode-languageserver';
|
||||
@@ -23,9 +22,8 @@ suite('JSON Document Symbols', () => {
|
||||
var symbolProvider = new JSONDocumentSymbols();
|
||||
|
||||
var document = ITextDocument.create(uri, value);
|
||||
var lines = createLinesModel(value);
|
||||
var jsonDoc = Parser.parse(value);
|
||||
return symbolProvider.compute(document, lines, jsonDoc);
|
||||
return symbolProvider.compute(document, jsonDoc);
|
||||
}
|
||||
|
||||
var assertOutline: any = function(actual: SymbolInformation[], expected: any[], message: string) {
|
||||
|
||||
@@ -5,10 +5,7 @@
|
||||
'use strict';
|
||||
|
||||
import Json = require('../json-toolbox/json');
|
||||
import {
|
||||
ITextDocument, DocumentFormattingParams, Range, Position, FormattingOptions, TextEdit
|
||||
} from 'vscode-languageserver';
|
||||
import {LinesModel, create as createLinesModel} from '../utils/lines';
|
||||
import {ITextDocument, DocumentFormattingParams, Range, Position, FormattingOptions, TextEdit} from 'vscode-languageserver';
|
||||
import Formatter = require('../jsonFormatter');
|
||||
import assert = require('assert');
|
||||
|
||||
@@ -18,29 +15,26 @@ suite('JSON Formatter', () => {
|
||||
let range: Range = null;
|
||||
let uri = 'test://test.json';
|
||||
|
||||
let lines = createLinesModel(unformatted);
|
||||
|
||||
let rangeStart = unformatted.indexOf('|');
|
||||
let rangeEnd = unformatted.lastIndexOf('|');
|
||||
if (rangeStart !== -1 && rangeEnd !== -1) {
|
||||
// remove '|'
|
||||
var unformattedDoc = ITextDocument.create(uri, unformatted);
|
||||
unformatted = unformatted.substring(0, rangeStart) + unformatted.substring(rangeStart + 1, rangeEnd) + unformatted.substring(rangeEnd + 1);
|
||||
let startPos = lines.positionAt(rangeStart);
|
||||
let endPos = lines.positionAt(rangeEnd);
|
||||
let startPos = unformattedDoc.positionAt(rangeStart);
|
||||
let endPos = unformattedDoc.positionAt(rangeEnd);
|
||||
range = Range.create(startPos, endPos);
|
||||
|
||||
lines = createLinesModel(unformatted);
|
||||
}
|
||||
|
||||
var document = ITextDocument.create(uri, unformatted);
|
||||
let edits = Formatter.format(document, lines, range, { tabSize: 2, insertSpaces: insertSpaces });
|
||||
let edits = Formatter.format(document, range, { tabSize: 2, insertSpaces: insertSpaces });
|
||||
|
||||
let formatted = unformatted;
|
||||
let sortedEdits = edits.sort((a, b) => lines.offsetAt(b.range.start) - lines.offsetAt(a.range.start));
|
||||
let sortedEdits = edits.sort((a, b) => document.offsetAt(b.range.start) - document.offsetAt(a.range.start));
|
||||
let lastOffset = formatted.length;
|
||||
sortedEdits.forEach(e => {
|
||||
let startOffset = lines.offsetAt(e.range.start);
|
||||
let endOffset = lines.offsetAt(e.range.end);
|
||||
let startOffset = document.offsetAt(e.range.start);
|
||||
let endOffset = document.offsetAt(e.range.end);
|
||||
assert.ok(startOffset <= endOffset);
|
||||
assert.ok(endOffset <= lastOffset);
|
||||
formatted = formatted.substring(0, startOffset) + e.newText + formatted.substring(endOffset, formatted.length);
|
||||
|
||||
@@ -10,7 +10,6 @@ import SchemaService = require('../jsonSchemaService');
|
||||
import JsonSchema = require('../json-toolbox/jsonSchema');
|
||||
import {JSONCompletion} from '../jsonCompletion';
|
||||
import {IXHROptions, IXHRResponse} from '../utils/httpRequest';
|
||||
import {create as createLinesModel} from '../utils/lines';
|
||||
import {JSONHover} from '../jsonHover';
|
||||
|
||||
import {Hover, ITextDocument, TextDocumentIdentifier, TextDocumentPosition, Range, Position, TextEdit} from 'vscode-languageserver';
|
||||
@@ -27,9 +26,8 @@ suite('JSON Hover', () => {
|
||||
|
||||
var document = ITextDocument.create(uri, value);
|
||||
var textDocumentLocation = TextDocumentPosition.create(uri, position);
|
||||
var lines = createLinesModel(value);
|
||||
var jsonDoc = Parser.parse(value);
|
||||
return hoverProvider.doHover(document, textDocumentLocation, lines, jsonDoc);
|
||||
return hoverProvider.doHover(document, textDocumentLocation, jsonDoc);
|
||||
}
|
||||
|
||||
var requestService = function(options: IXHROptions): Promise<IXHRResponse> {
|
||||
|
||||
@@ -1,81 +0,0 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
'use strict';
|
||||
|
||||
import assert = require('assert');
|
||||
import lines = require('../utils/lines');
|
||||
import {Position} from 'vscode-languageserver';
|
||||
|
||||
suite('Lines Model Validator', () => {
|
||||
|
||||
test('single line', () => {
|
||||
var str = "Hello World";
|
||||
var lm = lines.create(str);
|
||||
assert.equal(lm.lineCount, 1);
|
||||
|
||||
for (var i = 0; i < str.length; i++) {
|
||||
assert.equal(lm.offsetAt(Position.create(0, i)), i);
|
||||
assert.deepEqual(lm.positionAt(i), Position.create(0, i));
|
||||
}
|
||||
});
|
||||
|
||||
test('Mutiple lines', () => {
|
||||
var str = "ABCDE\nFGHIJ\nKLMNO\n";
|
||||
var lm = lines.create(str);
|
||||
assert.equal(lm.lineCount, 4);
|
||||
|
||||
for (var i = 0; i < str.length; i++) {
|
||||
var line = Math.floor(i / 6);
|
||||
var column = i % 6;
|
||||
|
||||
assert.equal(lm.offsetAt(Position.create(line, column)), i);
|
||||
assert.deepEqual(lm.positionAt(i), Position.create(line, column));
|
||||
}
|
||||
|
||||
assert.equal(lm.offsetAt(Position.create(3, 0)), 18);
|
||||
assert.equal(lm.offsetAt(Position.create(3, 1)), 18);
|
||||
assert.deepEqual(lm.positionAt(18), Position.create(3, 0));
|
||||
assert.deepEqual(lm.positionAt(19), Position.create(3, 0));
|
||||
});
|
||||
|
||||
test('New line characters', () => {
|
||||
var str = "ABCDE\rFGHIJ";
|
||||
assert.equal(lines.create(str).lineCount, 2);
|
||||
|
||||
var str = "ABCDE\nFGHIJ";
|
||||
assert.equal(lines.create(str).lineCount, 2);
|
||||
|
||||
var str = "ABCDE\r\nFGHIJ";
|
||||
assert.equal(lines.create(str).lineCount, 2);
|
||||
|
||||
str = "ABCDE\n\nFGHIJ";
|
||||
assert.equal(lines.create(str).lineCount, 3);
|
||||
|
||||
str = "ABCDE\r\rFGHIJ";
|
||||
assert.equal(lines.create(str).lineCount, 3);
|
||||
|
||||
str = "ABCDE\n\rFGHIJ";
|
||||
assert.equal(lines.create(str).lineCount, 3);
|
||||
})
|
||||
|
||||
|
||||
test('invalid inputs', () => {
|
||||
var str = "Hello World";
|
||||
var lm = lines.create(str);
|
||||
|
||||
// invalid position
|
||||
assert.equal(lm.offsetAt(Position.create(0, str.length)), str.length);
|
||||
assert.equal(lm.offsetAt(Position.create(0, str.length + 3)), str.length);
|
||||
assert.equal(lm.offsetAt(Position.create(2, 3)), str.length);
|
||||
assert.equal(lm.offsetAt(Position.create(-1, 3)), 0);
|
||||
assert.equal(lm.offsetAt(Position.create(0, -3)), 0);
|
||||
assert.equal(lm.offsetAt(Position.create(1, -3)), str.length);
|
||||
|
||||
// invalid offsets
|
||||
assert.deepEqual(lm.positionAt(-1), Position.create(0, 0));
|
||||
assert.deepEqual(lm.positionAt(str.length), Position.create(0, str.length));
|
||||
assert.deepEqual(lm.positionAt(str.length + 3), Position.create(0, str.length));
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user