From 9f6053242ff246857ee29514befc141dc111534b Mon Sep 17 00:00:00 2001 From: Johannes Rieken Date: Fri, 4 May 2018 18:24:01 +0200 Subject: [PATCH] type converters, use namespace for Position --- .../workbench/api/node/extHostApiCommands.ts | 20 ++++++++-------- src/vs/workbench/api/node/extHostCommands.ts | 2 +- .../api/node/extHostLanguageFeatures.ts | 24 +++++++++---------- .../workbench/api/node/extHostTextEditor.ts | 4 ++-- .../api/node/extHostTypeConverters.ts | 13 +++++----- 5 files changed, 32 insertions(+), 31 deletions(-) diff --git a/src/vs/workbench/api/node/extHostApiCommands.ts b/src/vs/workbench/api/node/extHostApiCommands.ts index 8198d64da6c..abde2a963ea 100644 --- a/src/vs/workbench/api/node/extHostApiCommands.ts +++ b/src/vs/workbench/api/node/extHostApiCommands.ts @@ -288,7 +288,7 @@ export class ExtHostApiCommands { private _executeDefinitionProvider(resource: URI, position: types.Position): Thenable { const args = { resource, - position: position && typeConverters.fromPosition(position) + position: position && typeConverters.Position.from(position) }; return this._commands.executeCommand('_executeDefinitionProvider', args) .then(tryMapWith(typeConverters.location.to)); @@ -297,7 +297,7 @@ export class ExtHostApiCommands { private _executeTypeDefinitionProvider(resource: URI, position: types.Position): Thenable { const args = { resource, - position: position && typeConverters.fromPosition(position) + position: position && typeConverters.Position.from(position) }; return this._commands.executeCommand('_executeTypeDefinitionProvider', args) .then(tryMapWith(typeConverters.location.to)); @@ -306,7 +306,7 @@ export class ExtHostApiCommands { private _executeImplementationProvider(resource: URI, position: types.Position): Thenable { const args = { resource, - position: position && typeConverters.fromPosition(position) + position: position && typeConverters.Position.from(position) }; return this._commands.executeCommand('_executeImplementationProvider', args) .then(tryMapWith(typeConverters.location.to)); @@ -315,7 +315,7 @@ export class ExtHostApiCommands { private _executeHoverProvider(resource: URI, position: types.Position): Thenable { const args = { resource, - position: position && typeConverters.fromPosition(position) + position: position && typeConverters.Position.from(position) }; return this._commands.executeCommand('_executeHoverProvider', args) .then(tryMapWith(typeConverters.toHover)); @@ -324,7 +324,7 @@ export class ExtHostApiCommands { private _executeDocumentHighlights(resource: URI, position: types.Position): Thenable { const args = { resource, - position: position && typeConverters.fromPosition(position) + position: position && typeConverters.Position.from(position) }; return this._commands.executeCommand('_executeDocumentHighlights', args) .then(tryMapWith(typeConverters.toDocumentHighlight)); @@ -333,7 +333,7 @@ export class ExtHostApiCommands { private _executeReferenceProvider(resource: URI, position: types.Position): Thenable { const args = { resource, - position: position && typeConverters.fromPosition(position) + position: position && typeConverters.Position.from(position) }; return this._commands.executeCommand('_executeReferenceProvider', args) .then(tryMapWith(typeConverters.location.to)); @@ -342,7 +342,7 @@ export class ExtHostApiCommands { private _executeDocumentRenameProvider(resource: URI, position: types.Position, newName: string): Thenable { const args = { resource, - position: position && typeConverters.fromPosition(position), + position: position && typeConverters.Position.from(position), newName }; return this._commands.executeCommand('_executeDocumentRenameProvider', args).then(value => { @@ -359,7 +359,7 @@ export class ExtHostApiCommands { private _executeSignatureHelpProvider(resource: URI, position: types.Position, triggerCharacter: string): Thenable { const args = { resource, - position: position && typeConverters.fromPosition(position), + position: position && typeConverters.Position.from(position), triggerCharacter }; return this._commands.executeCommand('_executeSignatureHelpProvider', args).then(value => { @@ -373,7 +373,7 @@ export class ExtHostApiCommands { private _executeCompletionItemProvider(resource: URI, position: types.Position, triggerCharacter: string, maxItemsToResolve: number): Thenable { const args = { resource, - position: position && typeConverters.fromPosition(position), + position: position && typeConverters.Position.from(position), triggerCharacter, maxItemsToResolve }; @@ -482,7 +482,7 @@ export class ExtHostApiCommands { private _executeFormatOnTypeProvider(resource: URI, position: types.Position, ch: string, options: vscode.FormattingOptions): Thenable { const args = { resource, - position: typeConverters.fromPosition(position), + position: typeConverters.Position.from(position), ch, options }; diff --git a/src/vs/workbench/api/node/extHostCommands.ts b/src/vs/workbench/api/node/extHostCommands.ts index b7927852c2a..e1607cd562e 100644 --- a/src/vs/workbench/api/node/extHostCommands.ts +++ b/src/vs/workbench/api/node/extHostCommands.ts @@ -92,7 +92,7 @@ export class ExtHostCommands implements ExtHostCommandsShape { args = cloneAndChange(args, function (value) { if (value instanceof extHostTypes.Position) { - return extHostTypeConverter.fromPosition(value); + return extHostTypeConverter.Position.from(value); } if (value instanceof extHostTypes.Range) { return extHostTypeConverter.Range.from(value); diff --git a/src/vs/workbench/api/node/extHostLanguageFeatures.ts b/src/vs/workbench/api/node/extHostLanguageFeatures.ts index f6acf421ee1..0b5d7475ecc 100644 --- a/src/vs/workbench/api/node/extHostLanguageFeatures.ts +++ b/src/vs/workbench/api/node/extHostLanguageFeatures.ts @@ -116,7 +116,7 @@ class DefinitionAdapter { provideDefinition(resource: URI, position: IPosition): TPromise { let doc = this._documents.getDocumentData(resource).document; - let pos = typeConvert.toPosition(position); + let pos = typeConvert.Position.to(position); return asWinJsPromise(token => this._provider.provideDefinition(doc, pos, token)).then(value => { if (Array.isArray(value)) { return value.map(typeConvert.location.from); @@ -139,7 +139,7 @@ class ImplementationAdapter { provideImplementation(resource: URI, position: IPosition): TPromise { let doc = this._documents.getDocumentData(resource).document; - let pos = typeConvert.toPosition(position); + let pos = typeConvert.Position.to(position); return asWinJsPromise(token => this._provider.provideImplementation(doc, pos, token)).then(value => { if (Array.isArray(value)) { return value.map(typeConvert.location.from); @@ -162,7 +162,7 @@ class TypeDefinitionAdapter { provideTypeDefinition(resource: URI, position: IPosition): TPromise { const doc = this._documents.getDocumentData(resource).document; - const pos = typeConvert.toPosition(position); + const pos = typeConvert.Position.to(position); return asWinJsPromise(token => this._provider.provideTypeDefinition(doc, pos, token)).then(value => { if (Array.isArray(value)) { return value.map(typeConvert.location.from); @@ -187,7 +187,7 @@ class HoverAdapter { public provideHover(resource: URI, position: IPosition): TPromise { let doc = this._documents.getDocumentData(resource).document; - let pos = typeConvert.toPosition(position); + let pos = typeConvert.Position.to(position); return asWinJsPromise(token => this._provider.provideHover(doc, pos, token)).then(value => { if (!value || isFalsyOrEmpty(value.contents)) { @@ -218,7 +218,7 @@ class DocumentHighlightAdapter { provideDocumentHighlights(resource: URI, position: IPosition): TPromise { let doc = this._documents.getDocumentData(resource).document; - let pos = typeConvert.toPosition(position); + let pos = typeConvert.Position.to(position); return asWinJsPromise(token => this._provider.provideDocumentHighlights(doc, pos, token)).then(value => { if (Array.isArray(value)) { @@ -248,7 +248,7 @@ class ReferenceAdapter { provideReferences(resource: URI, position: IPosition, context: modes.ReferenceContext): TPromise { let doc = this._documents.getDocumentData(resource).document; - let pos = typeConvert.toPosition(position); + let pos = typeConvert.Position.to(position); return asWinJsPromise(token => this._provider.provideReferences(doc, pos, context, token)).then(value => { if (Array.isArray(value)) { @@ -389,7 +389,7 @@ class OnTypeFormattingAdapter { provideOnTypeFormattingEdits(resource: URI, position: IPosition, ch: string, options: modes.FormattingOptions): TPromise { const { document } = this._documents.getDocumentData(resource); - const pos = typeConvert.toPosition(position); + const pos = typeConvert.Position.to(position); return asWinJsPromise(token => this._provider.provideOnTypeFormattingEdits(document, pos, ch, options, token)).then(value => { if (Array.isArray(value)) { @@ -479,7 +479,7 @@ class RenameAdapter { provideRenameEdits(resource: URI, position: IPosition, newName: string): TPromise { let doc = this._documents.getDocumentData(resource).document; - let pos = typeConvert.toPosition(position); + let pos = typeConvert.Position.to(position); return asWinJsPromise(token => this._provider.provideRenameEdits(doc, pos, newName, token)).then(value => { if (!value) { @@ -510,7 +510,7 @@ class RenameAdapter { } let doc = this._documents.getDocumentData(resource).document; - let pos = typeConvert.toPosition(position); + let pos = typeConvert.Position.to(position); return asWinJsPromise(token => this._provider.prepareRename(doc, pos, token)).then(rangeOrLocation => { @@ -560,7 +560,7 @@ class SuggestAdapter { provideCompletionItems(resource: URI, position: IPosition, context: modes.SuggestContext): TPromise { const doc = this._documents.getDocumentData(resource).document; - const pos = typeConvert.toPosition(position); + const pos = typeConvert.Position.to(position); return asWinJsPromise(token => { return this._provider.provideCompletionItems(doc, pos, token, typeConvert.CompletionContext.from(context)); @@ -623,7 +623,7 @@ class SuggestAdapter { } const doc = this._documents.getDocumentData(resource).document; - const pos = typeConvert.toPosition(position); + const pos = typeConvert.Position.to(position); const wordRangeBeforePos = (doc.getWordRangeAtPosition(pos) as Range || new Range(pos, pos)).with({ end: pos }); const newSuggestion = this._convertCompletionItem(resolvedItem, pos, wordRangeBeforePos, _id, _parentId); if (newSuggestion) { @@ -714,7 +714,7 @@ class SignatureHelpAdapter { provideSignatureHelp(resource: URI, position: IPosition): TPromise { const doc = this._documents.getDocumentData(resource).document; - const pos = typeConvert.toPosition(position); + const pos = typeConvert.Position.to(position); return asWinJsPromise(token => this._provider.provideSignatureHelp(doc, pos, token)).then(value => { if (value) { diff --git a/src/vs/workbench/api/node/extHostTextEditor.ts b/src/vs/workbench/api/node/extHostTextEditor.ts index 25c4c077c98..eacb6e2d37d 100644 --- a/src/vs/workbench/api/node/extHostTextEditor.ts +++ b/src/vs/workbench/api/node/extHostTextEditor.ts @@ -553,7 +553,7 @@ export class ExtHostTextEditor implements vscode.TextEditor { ranges = this._selections.map(TypeConverters.Range.from); } else if (where instanceof Position) { - const { lineNumber, column } = TypeConverters.fromPosition(where); + const { lineNumber, column } = TypeConverters.Position.from(where); ranges = [{ startLineNumber: lineNumber, startColumn: column, endLineNumber: lineNumber, endColumn: column }]; } else if (where instanceof Range) { @@ -564,7 +564,7 @@ export class ExtHostTextEditor implements vscode.TextEditor { if (posOrRange instanceof Range) { ranges.push(TypeConverters.Range.from(posOrRange)); } else { - const { lineNumber, column } = TypeConverters.fromPosition(posOrRange); + const { lineNumber, column } = TypeConverters.Position.from(posOrRange); ranges.push({ startLineNumber: lineNumber, startColumn: column, endLineNumber: lineNumber, endColumn: column }); } } diff --git a/src/vs/workbench/api/node/extHostTypeConverters.ts b/src/vs/workbench/api/node/extHostTypeConverters.ts index cdbff046b15..5b671d2c54c 100644 --- a/src/vs/workbench/api/node/extHostTypeConverters.ts +++ b/src/vs/workbench/api/node/extHostTypeConverters.ts @@ -79,12 +79,13 @@ export namespace Range { } } -export function toPosition(position: IPosition): types.Position { - return new types.Position(position.lineNumber - 1, position.column - 1); -} - -export function fromPosition(position: types.Position): IPosition { - return { lineNumber: position.line + 1, column: position.character + 1 }; +export namespace Position { + export function to(position: IPosition): types.Position { + return new types.Position(position.lineNumber - 1, position.column - 1); + } + export function from(position: types.Position): IPosition { + return { lineNumber: position.line + 1, column: position.character + 1 }; + } } export function fromDiagnostic(value: vscode.Diagnostic): IMarkerData {