提交 474a29ee 编写于 作者: M Matt Bierner

Replace duplicated textSpan conversion logic

上级 ce3c4b3c
......@@ -7,6 +7,7 @@ import { CodeLensProvider, CodeLens, CancellationToken, TextDocument, Range, Uri
import * as Proto from '../protocol';
import { ITypescriptServiceClient } from '../typescriptService';
import { textSpanToRange } from '../utils/convert';
export class ReferencesCodeLens extends CodeLens {
constructor(
......@@ -99,10 +100,7 @@ export abstract class TypeScriptBaseCodeLensProvider implements CodeLensProvider
return null;
}
const range = new Range(
span.start.line - 1, span.start.offset - 1,
span.end.line - 1, span.end.offset - 1);
const range = textSpanToRange(span);
const text = document.getText(range);
const identifierMatch = new RegExp(`^(.*?(\\b|\\W))${(item.text || '').replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&')}(\\b|\\W)`, 'gm');
......
......@@ -7,6 +7,7 @@ import { CodeActionProvider, TextDocument, Range, CancellationToken, CodeActionC
import * as Proto from '../protocol';
import { ITypescriptServiceClient } from '../typescriptService';
import { textSpanToRange } from '../utils/convert';
interface NumberSet {
[key: number]: boolean;
......@@ -112,9 +113,7 @@ export default class TypeScriptCodeActionProvider implements CodeActionProvider
for (const change of action.changes) {
for (const textChange of change.textChanges) {
workspaceEdit.replace(this.client.asUrl(change.fileName),
new Range(
textChange.start.line - 1, textChange.start.offset - 1,
textChange.end.line - 1, textChange.end.offset - 1),
textSpanToRange(textChange),
textChange.newText);
}
}
......
......@@ -11,6 +11,7 @@ import TypingsStatus from '../utils/typingsStatus';
import * as PConst from '../protocol.const';
import { CompletionEntry, CompletionsRequestArgs, CompletionDetailsRequestArgs, CompletionEntryDetails, FileLocationRequestArgs } from '../protocol';
import * as Previewer from './previewer';
import { textSpanToRange } from '../utils/convert';
import * as nls from 'vscode-nls';
let localize = nls.loadMessageBundle();
......@@ -32,7 +33,7 @@ class MyCompletionItem extends CompletionItem {
let span: protocol.TextSpan = entry.replacementSpan;
// The indexing for the range returned by the server uses 1-based indexing.
// We convert to 0-based indexing.
this.textEdit = TextEdit.replace(new Range(span.start.line - 1, span.start.offset - 1, span.end.line - 1, span.end.offset - 1), entry.name);
this.textEdit = TextEdit.replace(textSpanToRange(span), entry.name);
} else {
// Try getting longer, prefix based range for completions that span words
const wordRange = document.getWordRangeAtPosition(position);
......
......@@ -3,10 +3,11 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { TextDocument, Position, Range, CancellationToken, Location } from 'vscode';
import { TextDocument, Position, CancellationToken, Location } from 'vscode';
import * as Proto from '../protocol';
import { ITypescriptServiceClient } from '../typescriptService';
import { textSpanToRange } from '../utils/convert';
export default class TypeScriptDefinitionProviderBase {
constructor(
......@@ -37,7 +38,7 @@ export default class TypeScriptDefinitionProviderBase {
if (resource === null) {
return null;
} else {
return new Location(resource, new Range(location.start.line - 1, location.start.offset - 1, location.end.line - 1, location.end.offset - 1));
return new Location(resource, textSpanToRange(location));
}
}).filter(x => x !== null) as Location[];
}, () => {
......
......@@ -7,6 +7,7 @@ import { DocumentHighlightProvider, DocumentHighlight, DocumentHighlightKind, Te
import * as Proto from '../protocol';
import { ITypescriptServiceClient } from '../typescriptService';
import { textSpanToRange } from '../utils/convert';
export default class TypeScriptDocumentHighlightProvider implements DocumentHighlightProvider {
......@@ -37,10 +38,10 @@ export default class TypeScriptDocumentHighlightProvider implements DocumentHigh
return [];
}
}
return data.map((item) => {
return new DocumentHighlight(new Range(item.start.line - 1, item.start.offset - 1, item.end.line - 1, item.end.offset - 1),
item.isWriteAccess ? DocumentHighlightKind.Write : DocumentHighlightKind.Read);
});
return data.map(item =>
new DocumentHighlight(
textSpanToRange(item),
item.isWriteAccess ? DocumentHighlightKind.Write : DocumentHighlightKind.Read));
}
return [];
}, () => {
......
......@@ -3,11 +3,12 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { DocumentSymbolProvider, SymbolInformation, SymbolKind, TextDocument, Range, Location, CancellationToken, Uri } from 'vscode';
import { DocumentSymbolProvider, SymbolInformation, SymbolKind, TextDocument, Location, CancellationToken, Uri } from 'vscode';
import * as Proto from '../protocol';
import * as PConst from '../protocol.const';
import { ITypescriptServiceClient } from '../typescriptService';
import { textSpanToRange } from '../utils/convert';
const outlineTypeTable: { [kind: string]: SymbolKind } = Object.create(null);
outlineTypeTable[PConst.Kind.module] = SymbolKind.Module;
......@@ -25,9 +26,6 @@ outlineTypeTable[PConst.Kind.variable] = SymbolKind.Variable;
outlineTypeTable[PConst.Kind.function] = SymbolKind.Function;
outlineTypeTable[PConst.Kind.localFunction] = SymbolKind.Function;
function textSpan2Range(value: Proto.TextSpan): Range {
return new Range(value.start.line - 1, value.start.offset - 1, value.end.line - 1, value.end.offset - 1);
}
export default class TypeScriptDocumentSymbolProvider implements DocumentSymbolProvider {
public constructor(
......@@ -73,7 +71,7 @@ export default class TypeScriptDocumentSymbolProvider implements DocumentSymbolP
let result = new SymbolInformation(item.text,
outlineTypeTable[item.kind as string] || SymbolKind.Variable,
containerLabel ? containerLabel : '',
new Location(resource, textSpan2Range(item.spans[0])));
new Location(resource, textSpanToRange(item.spans[0])));
foldingMap[key] = result;
bucket.push(result);
}
......@@ -88,7 +86,7 @@ export default class TypeScriptDocumentSymbolProvider implements DocumentSymbolP
const result = new SymbolInformation(item.text,
outlineTypeTable[item.kind as string] || SymbolKind.Variable,
containerLabel ? containerLabel : '',
new Location(resource, textSpan2Range(item.spans[0]))
new Location(resource, textSpanToRange(item.spans[0]))
);
if (item.childItems && item.childItems.length > 0) {
for (const child of item.childItems) {
......
......@@ -7,6 +7,7 @@ import { workspace as Workspace, DocumentRangeFormattingEditProvider, OnTypeForm
import * as Proto from '../protocol';
import { ITypescriptServiceClient } from '../typescriptService';
import { textSpanToRange } from '../utils/convert';
interface Configuration {
enable: boolean;
......@@ -205,8 +206,7 @@ export class TypeScriptFormattingProvider implements DocumentRangeFormattingEdit
}
private codeEdit2SingleEditOperation(edit: Proto.CodeEdit): TextEdit {
return new TextEdit(new Range(edit.start.line - 1, edit.start.offset - 1, edit.end.line - 1, edit.end.offset - 1),
edit.newText);
return new TextEdit(textSpanToRange(edit), edit.newText);
}
private getFormatOptions(options: FormattingOptions): Proto.FormatCodeSettings {
......
......@@ -3,11 +3,12 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { HoverProvider, Hover, TextDocument, Position, Range, CancellationToken } from 'vscode';
import { HoverProvider, Hover, TextDocument, Position, CancellationToken } from 'vscode';
import * as Proto from '../protocol';
import { ITypescriptServiceClient } from '../typescriptService';
import { tagsMarkdownPreview } from './previewer';
import { textSpanToRange } from '../utils/convert';
export default class TypeScriptHoverProvider implements HoverProvider {
......@@ -31,7 +32,7 @@ export default class TypeScriptHoverProvider implements HoverProvider {
const data = response.body;
return new Hover(
TypeScriptHoverProvider.getContents(data),
new Range(data.start.line - 1, data.start.offset - 1, data.end.line - 1, data.end.offset - 1));
textSpanToRange(data));
}
} catch (e) {
// noop
......
......@@ -9,6 +9,7 @@ import * as PConst from '../protocol.const';
import { TypeScriptBaseCodeLensProvider, ReferencesCodeLens } from './baseCodeLensProvider';
import { ITypescriptServiceClient } from '../typescriptService';
import { textSpanToRange } from '../utils/convert';
import * as nls from 'vscode-nls';
const localize = nls.loadMessageBundle();
......@@ -50,9 +51,7 @@ export default class TypeScriptImplementationsCodeLensProvider extends TypeScrip
// Only take first line on implementation: https://github.com/Microsoft/vscode/issues/23924
new Location(this.client.asUrl(reference.file),
reference.start.line === reference.end.line
? new Range(
reference.start.line - 1, reference.start.offset - 1,
reference.end.line - 1, reference.end.offset - 1)
? textSpanToRange(reference)
: new Range(
reference.start.line - 1, reference.start.offset - 1,
reference.start.line, 0)))
......
......@@ -9,6 +9,7 @@ import { CodeActionProvider, TextDocument, Range, CancellationToken, CodeActionC
import * as Proto from '../protocol';
import { ITypescriptServiceClient } from '../typescriptService';
import { textSpanToRange } from '../utils/convert';
export default class TypeScriptRefactorProvider implements CodeActionProvider {
......@@ -85,9 +86,7 @@ export default class TypeScriptRefactorProvider implements CodeActionProvider {
for (const edit of edits) {
for (const textChange of edit.textChanges) {
workspaceEdit.replace(this.client.asUrl(edit.fileName),
new Range(
textChange.start.line - 1, textChange.start.offset - 1,
textChange.end.line - 1, textChange.end.offset - 1),
textSpanToRange(textChange),
textChange.newText);
}
}
......
......@@ -3,10 +3,11 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { ReferenceProvider, Location, TextDocument, Position, Range, CancellationToken } from 'vscode';
import { ReferenceProvider, Location, TextDocument, Position, CancellationToken } from 'vscode';
import * as Proto from '../protocol';
import { ITypescriptServiceClient } from '../typescriptService';
import { textSpanToRange } from '../utils/convert';
export default class TypeScriptReferenceSupport implements ReferenceProvider {
public constructor(
......@@ -35,9 +36,7 @@ export default class TypeScriptReferenceSupport implements ReferenceProvider {
continue;
}
const url = this.client.asUrl(ref.file);
const location = new Location(
url,
new Range(ref.start.line - 1, ref.start.offset - 1, ref.end.line - 1, ref.end.offset - 1));
const location = new Location(url, textSpanToRange(ref));
result.push(location);
}
return result;
......
......@@ -9,6 +9,7 @@ import * as PConst from '../protocol.const';
import { TypeScriptBaseCodeLensProvider, ReferencesCodeLens } from './baseCodeLensProvider';
import { ITypescriptServiceClient } from '../typescriptService';
import { textSpanToRange } from '../utils/convert';
import * as nls from 'vscode-nls';
const localize = nls.loadMessageBundle();
......@@ -47,10 +48,7 @@ export default class TypeScriptReferencesCodeLensProvider extends TypeScriptBase
const locations = response.body.refs
.map(reference =>
new Location(this.client.asUrl(reference.file),
new Range(
reference.start.line - 1, reference.start.offset - 1,
reference.end.line - 1, reference.end.offset - 1)))
new Location(this.client.asUrl(reference.file), textSpanToRange(reference)))
.filter(location =>
// Exclude original definition from references
!(location.uri.fsPath === codeLens.document.fsPath &&
......
......@@ -3,10 +3,11 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { RenameProvider, WorkspaceEdit, TextDocument, Position, Range, CancellationToken } from 'vscode';
import { RenameProvider, WorkspaceEdit, TextDocument, Position, CancellationToken } from 'vscode';
import * as Proto from '../protocol';
import { ITypescriptServiceClient } from '../typescriptService';
import { textSpanToRange } from '../utils/convert';
export default class TypeScriptRenameProvider implements RenameProvider {
public constructor(
......@@ -49,9 +50,7 @@ export default class TypeScriptRenameProvider implements RenameProvider {
continue;
}
for (const textSpan of spanGroup.locs) {
result.replace(resource,
new Range(textSpan.start.line - 1, textSpan.start.offset - 1, textSpan.end.line - 1, textSpan.end.offset - 1),
newName);
result.replace(resource, textSpanToRange(textSpan), newName);
}
}
return result;
......
......@@ -3,10 +3,11 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { workspace, window, Uri, WorkspaceSymbolProvider, SymbolInformation, SymbolKind, Range, Location, CancellationToken } from 'vscode';
import { workspace, window, Uri, WorkspaceSymbolProvider, SymbolInformation, SymbolKind, Location, CancellationToken } from 'vscode';
import * as Proto from '../protocol';
import { ITypescriptServiceClient } from '../typescriptService';
import { textSpanToRange } from '../utils/convert';
function getSymbolKind(item: Proto.NavtoItem): SymbolKind {
switch (item.kind) {
......@@ -67,7 +68,7 @@ export default class TypeScriptWorkspaceSymbolProvider implements WorkspaceSymbo
if (!item.containerName && item.kind === 'alias') {
continue;
}
const range = new Range(item.start.line - 1, item.start.offset - 1, item.end.line - 1, item.end.offset - 1);
const range = textSpanToRange(item);
let label = item.name;
if (item.kind === 'method' || item.kind === 'function') {
label += '()';
......
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Range } from 'vscode';
import * as Proto from '../protocol';
export const textSpanToRange = (span: Proto.TextSpan) =>
new Range(
span.start.line - 1, span.start.offset - 1,
span.end.line - 1, span.end.offset - 1);
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册