extHostTypeConverters.ts 14.2 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  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 Severity from 'vs/base/common/severity';
8
import * as modes from 'vs/editor/common/modes';
9
import * as types from './extHostTypes';
10
import { Position as EditorPosition } from 'vs/platform/editor/common/editor';
A
Alex Dima 已提交
11
import { IDecorationOptions, EndOfLineSequence } from 'vs/editor/common/editorCommon';
J
Johannes Rieken 已提交
12
import * as vscode from 'vscode';
13
import URI from 'vs/base/common/uri';
J
Johannes Rieken 已提交
14
import { ProgressLocation as MainProgressLocation } from 'vs/platform/progress/common/progress';
15
import { SaveReason } from 'vs/workbench/services/textfile/common/textfiles';
16 17 18
import { IPosition } from 'vs/editor/common/core/position';
import { IRange } from 'vs/editor/common/core/range';
import { ISelection } from 'vs/editor/common/core/selection';
E
Erich Gamma 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

export interface PositionLike {
	line: number;
	character: number;
}

export interface RangeLike {
	start: PositionLike;
	end: PositionLike;
}

export interface SelectionLike extends RangeLike {
	anchor: PositionLike;
	active: PositionLike;
}

35
export function toSelection(selection: ISelection): types.Selection {
36
	let { selectionStartLineNumber, selectionStartColumn, positionLineNumber, positionColumn } = selection;
37 38 39
	let start = new types.Position(selectionStartLineNumber - 1, selectionStartColumn - 1);
	let end = new types.Position(positionLineNumber - 1, positionColumn - 1);
	return new types.Selection(start, end);
E
Erich Gamma 已提交
40 41 42
}

export function fromSelection(selection: SelectionLike): ISelection {
43
	let { anchor, active } = selection;
E
Erich Gamma 已提交
44 45 46 47 48 49 50 51 52
	return {
		selectionStartLineNumber: anchor.line + 1,
		selectionStartColumn: anchor.character + 1,
		positionLineNumber: active.line + 1,
		positionColumn: active.character + 1
	};
}

export function fromRange(range: RangeLike): IRange {
J
Johannes Rieken 已提交
53 54 55
	if (!range) {
		return undefined;
	}
56
	let { start, end } = range;
E
Erich Gamma 已提交
57 58 59 60 61 62 63 64
	return {
		startLineNumber: start.line + 1,
		startColumn: start.character + 1,
		endLineNumber: end.line + 1,
		endColumn: end.character + 1
	};
}

65
export function toRange(range: IRange): types.Range {
J
Johannes Rieken 已提交
66 67 68
	if (!range) {
		return undefined;
	}
69
	let { startLineNumber, startColumn, endLineNumber, endColumn } = range;
70
	return new types.Range(startLineNumber - 1, startColumn - 1, endLineNumber - 1, endColumn - 1);
E
Erich Gamma 已提交
71 72
}

73 74
export function toPosition(position: IPosition): types.Position {
	return new types.Position(position.lineNumber - 1, position.column - 1);
E
Erich Gamma 已提交
75 76
}

77 78
export function fromPosition(position: types.Position): IPosition {
	return { lineNumber: position.line + 1, column: position.character + 1 };
79 80
}

E
Erich Gamma 已提交
81 82
export function fromDiagnosticSeverity(value: number): Severity {
	switch (value) {
83
		case types.DiagnosticSeverity.Error:
E
Erich Gamma 已提交
84
			return Severity.Error;
85
		case types.DiagnosticSeverity.Warning:
E
Erich Gamma 已提交
86
			return Severity.Warning;
87
		case types.DiagnosticSeverity.Information:
E
Erich Gamma 已提交
88
			return Severity.Info;
89
		case types.DiagnosticSeverity.Hint:
E
Erich Gamma 已提交
90 91 92 93 94
			return Severity.Ignore;
	}
	return Severity.Error;
}

95
export function toDiagnosticSeverty(value: Severity): types.DiagnosticSeverity {
E
Erich Gamma 已提交
96 97
	switch (value) {
		case Severity.Info:
98
			return types.DiagnosticSeverity.Information;
E
Erich Gamma 已提交
99
		case Severity.Warning:
100
			return types.DiagnosticSeverity.Warning;
E
Erich Gamma 已提交
101
		case Severity.Error:
102
			return types.DiagnosticSeverity.Error;
E
Erich Gamma 已提交
103
		case Severity.Ignore:
104
			return types.DiagnosticSeverity.Hint;
E
Erich Gamma 已提交
105
	}
106
	return types.DiagnosticSeverity.Error;
E
Erich Gamma 已提交
107 108 109
}

export function fromViewColumn(column?: vscode.ViewColumn): EditorPosition {
B
Benjamin Pasero 已提交
110
	let editorColumn = EditorPosition.ONE;
E
Erich Gamma 已提交
111
	if (typeof column !== 'number') {
B
Benjamin Pasero 已提交
112
		// stick with ONE
113
	} else if (column === <number>types.ViewColumn.Two) {
B
Benjamin Pasero 已提交
114
		editorColumn = EditorPosition.TWO;
115
	} else if (column === <number>types.ViewColumn.Three) {
B
Benjamin Pasero 已提交
116
		editorColumn = EditorPosition.THREE;
E
Erich Gamma 已提交
117 118 119 120
	}
	return editorColumn;
}

121 122
export function toViewColumn(position?: EditorPosition): vscode.ViewColumn {
	if (typeof position !== 'number') {
M
Matt Bierner 已提交
123
		return undefined;
124
	}
B
Benjamin Pasero 已提交
125
	if (position === EditorPosition.ONE) {
126
		return <number>types.ViewColumn.One;
B
Benjamin Pasero 已提交
127
	} else if (position === EditorPosition.TWO) {
128
		return <number>types.ViewColumn.Two;
B
Benjamin Pasero 已提交
129
	} else if (position === EditorPosition.THREE) {
130
		return <number>types.ViewColumn.Three;
131
	}
M
Matt Bierner 已提交
132
	return undefined;
133
}
E
Erich Gamma 已提交
134

M
Martin Aeschlimann 已提交
135
function isDecorationOptions(something: any): something is vscode.DecorationOptions {
136
	return (typeof something.range !== 'undefined');
E
Erich Gamma 已提交
137 138
}

139
function isDecorationOptionsArr(something: vscode.Range[] | vscode.DecorationOptions[]): something is vscode.DecorationOptions[] {
E
Erich Gamma 已提交
140 141 142
	if (something.length === 0) {
		return true;
	}
M
Martin Aeschlimann 已提交
143
	return isDecorationOptions(something[0]) ? true : false;
E
Erich Gamma 已提交
144 145
}

146 147 148 149 150 151 152 153 154 155 156
export namespace MarkedString {
	export function from(markup: vscode.MarkedString): string {
		if (typeof markup === 'string' || !markup) {
			return <string>markup;
		} else {
			const { language, value } = markup;
			return '```' + language + '\n' + value + '\n```';
		}
	}
}

157
export function fromRangeOrRangeWithMessage(ranges: vscode.Range[] | vscode.DecorationOptions[]): IDecorationOptions[] {
M
Martin Aeschlimann 已提交
158 159
	if (isDecorationOptionsArr(ranges)) {
		return ranges.map((r): IDecorationOptions => {
E
Erich Gamma 已提交
160 161
			return {
				range: fromRange(r.range),
162
				hoverMessage: Array.isArray(r.hoverMessage) ? r.hoverMessage.map(MarkedString.from) : MarkedString.from(r.hoverMessage),
163
				renderOptions: <any> /* URI vs Uri */r.renderOptions
E
Erich Gamma 已提交
164 165 166
			};
		});
	} else {
M
Martin Aeschlimann 已提交
167
		return ranges.map((r): IDecorationOptions => {
E
Erich Gamma 已提交
168 169
			return {
				range: fromRange(r)
B
Benjamin Pasero 已提交
170
			};
E
Erich Gamma 已提交
171 172 173
		});
	}
}
174

175
export const TextEdit = {
176

J
Johannes Rieken 已提交
177 178
	from(edit: vscode.TextEdit): modes.TextEdit {
		return <modes.TextEdit>{
179
			text: edit.newText,
J
Johannes Rieken 已提交
180
			eol: EndOfLine.from(edit.newEol),
181
			range: fromRange(edit.range)
B
Benjamin Pasero 已提交
182
		};
183
	},
J
Johannes Rieken 已提交
184 185 186 187
	to(edit: modes.TextEdit): vscode.TextEdit {
		let result = new types.TextEdit(toRange(edit.range), edit.text);
		result.newEol = EndOfLine.to(edit.eol);
		return result;
188
	}
B
Benjamin Pasero 已提交
189
};
190

191

192 193 194
export namespace SymbolKind {

	const _fromMapping: { [kind: number]: modes.SymbolKind } = Object.create(null);
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
	_fromMapping[types.SymbolKind.File] = modes.SymbolKind.File;
	_fromMapping[types.SymbolKind.Module] = modes.SymbolKind.Module;
	_fromMapping[types.SymbolKind.Namespace] = modes.SymbolKind.Namespace;
	_fromMapping[types.SymbolKind.Package] = modes.SymbolKind.Package;
	_fromMapping[types.SymbolKind.Class] = modes.SymbolKind.Class;
	_fromMapping[types.SymbolKind.Method] = modes.SymbolKind.Method;
	_fromMapping[types.SymbolKind.Property] = modes.SymbolKind.Property;
	_fromMapping[types.SymbolKind.Field] = modes.SymbolKind.Field;
	_fromMapping[types.SymbolKind.Constructor] = modes.SymbolKind.Constructor;
	_fromMapping[types.SymbolKind.Enum] = modes.SymbolKind.Enum;
	_fromMapping[types.SymbolKind.Interface] = modes.SymbolKind.Interface;
	_fromMapping[types.SymbolKind.Function] = modes.SymbolKind.Function;
	_fromMapping[types.SymbolKind.Variable] = modes.SymbolKind.Variable;
	_fromMapping[types.SymbolKind.Constant] = modes.SymbolKind.Constant;
	_fromMapping[types.SymbolKind.String] = modes.SymbolKind.String;
	_fromMapping[types.SymbolKind.Number] = modes.SymbolKind.Number;
	_fromMapping[types.SymbolKind.Boolean] = modes.SymbolKind.Boolean;
	_fromMapping[types.SymbolKind.Array] = modes.SymbolKind.Array;
	_fromMapping[types.SymbolKind.Object] = modes.SymbolKind.Object;
	_fromMapping[types.SymbolKind.Key] = modes.SymbolKind.Key;
	_fromMapping[types.SymbolKind.Null] = modes.SymbolKind.Null;
	_fromMapping[types.SymbolKind.EnumMember] = modes.SymbolKind.EnumMember;
	_fromMapping[types.SymbolKind.Struct] = modes.SymbolKind.Struct;
218 219
	_fromMapping[types.SymbolKind.Event] = modes.SymbolKind.Event;
	_fromMapping[types.SymbolKind.Operator] = modes.SymbolKind.Operator;
220
	_fromMapping[types.SymbolKind.TypeParameter] = modes.SymbolKind.TypeParameter;
221 222

	export function from(kind: vscode.SymbolKind): modes.SymbolKind {
223
		return _fromMapping[kind] || modes.SymbolKind.Property;
224 225
	}

226 227 228 229
	export function to(kind: modes.SymbolKind): vscode.SymbolKind {
		for (let k in _fromMapping) {
			if (_fromMapping[k] === kind) {
				return Number(k);
230
			}
231 232
		}
		return types.SymbolKind.Property;
233 234 235
	}
}

236 237
export function fromSymbolInformation(info: vscode.SymbolInformation): modes.SymbolInformation {
	return <modes.SymbolInformation>{
238
		name: info.name,
239
		kind: SymbolKind.from(info.kind),
240
		containerName: info.containerName,
241
		location: location.from(info.location)
242 243 244
	};
}

245 246 247 248
export function toSymbolInformation(bearing: modes.SymbolInformation): types.SymbolInformation {
	return new types.SymbolInformation(
		bearing.name,
		SymbolKind.to(bearing.kind),
249
		bearing.containerName,
250
		location.to(bearing.location)
251
	);
252 253 254
}


255
export const location = {
J
Johannes Rieken 已提交
256
	from(value: vscode.Location): modes.Location {
257
		return {
J
Johannes Rieken 已提交
258 259
			range: value.range && fromRange(value.range),
			uri: <URI>value.uri
J
Johannes Rieken 已提交
260
		};
261
	},
262 263
	to(value: modes.Location): types.Location {
		return new types.Location(value.uri, toRange(value.range));
264
	}
J
Johannes Rieken 已提交
265
};
266

267 268
export function fromHover(hover: vscode.Hover): modes.Hover {
	return <modes.Hover>{
269
		range: fromRange(hover.range),
270
		contents: hover.contents.map(MarkedString.from)
B
Benjamin Pasero 已提交
271
	};
272 273
}

274
export function toHover(info: modes.Hover): types.Hover {
275
	return new types.Hover(info.contents, toRange(info.range));
276 277
}

278 279
export function toDocumentHighlight(occurrence: modes.DocumentHighlight): types.DocumentHighlight {
	return new types.DocumentHighlight(toRange(occurrence.range), occurrence.kind);
280 281
}

J
Johannes Rieken 已提交
282 283 284 285
export const CompletionItemKind = {

	from(kind: types.CompletionItemKind): modes.SuggestionType {
		switch (kind) {
286
			case types.CompletionItemKind.Method: return 'method';
J
Johannes Rieken 已提交
287 288 289 290 291 292
			case types.CompletionItemKind.Function: return 'function';
			case types.CompletionItemKind.Constructor: return 'constructor';
			case types.CompletionItemKind.Field: return 'field';
			case types.CompletionItemKind.Variable: return 'variable';
			case types.CompletionItemKind.Class: return 'class';
			case types.CompletionItemKind.Interface: return 'interface';
293
			case types.CompletionItemKind.Struct: return 'struct';
J
Johannes Rieken 已提交
294 295 296 297
			case types.CompletionItemKind.Module: return 'module';
			case types.CompletionItemKind.Property: return 'property';
			case types.CompletionItemKind.Unit: return 'unit';
			case types.CompletionItemKind.Value: return 'value';
298
			case types.CompletionItemKind.Constant: return 'constant';
J
Johannes Rieken 已提交
299
			case types.CompletionItemKind.Enum: return 'enum';
300
			case types.CompletionItemKind.EnumMember: return 'enum-member';
J
Johannes Rieken 已提交
301 302 303 304 305 306
			case types.CompletionItemKind.Keyword: return 'keyword';
			case types.CompletionItemKind.Snippet: return 'snippet';
			case types.CompletionItemKind.Text: return 'text';
			case types.CompletionItemKind.Color: return 'color';
			case types.CompletionItemKind.File: return 'file';
			case types.CompletionItemKind.Reference: return 'reference';
307
			case types.CompletionItemKind.Folder: return 'folder';
308 309
			case types.CompletionItemKind.Event: return 'event';
			case types.CompletionItemKind.Operator: return 'operator';
310
			case types.CompletionItemKind.TypeParameter: return 'type-parameter';
J
Johannes Rieken 已提交
311
		}
312
		return 'property';
J
Johannes Rieken 已提交
313 314 315 316
	},

	to(type: modes.SuggestionType): types.CompletionItemKind {
		if (!type) {
317
			return types.CompletionItemKind.Property;
J
Johannes Rieken 已提交
318 319 320 321 322 323
		} else {
			return types.CompletionItemKind[type.charAt(0).toUpperCase() + type.substr(1)];
		}
	}
};

324
export namespace Suggest {
325

326
	export function to(position: types.Position, suggestion: modes.ISuggestion): types.CompletionItem {
327
		const result = new types.CompletionItem(suggestion.label);
328
		result.insertText = suggestion.insertText;
J
Johannes Rieken 已提交
329
		result.kind = CompletionItemKind.to(suggestion.type);
330 331
		result.detail = suggestion.detail;
		result.documentation = suggestion.documentation;
332 333
		result.sortText = suggestion.sortText;
		result.filterText = suggestion.filterText;
334

335
		// 'overwrite[Before|After]'-logic
336
		let overwriteBefore = (typeof suggestion.overwriteBefore === 'number') ? suggestion.overwriteBefore : 0;
M
Martin Aeschlimann 已提交
337 338 339
		let startPosition = new types.Position(position.line, Math.max(0, position.character - overwriteBefore));
		let endPosition = position;
		if (typeof suggestion.overwriteAfter === 'number') {
340 341
			endPosition = new types.Position(position.line, position.character + suggestion.overwriteAfter);
		}
342 343 344 345 346 347 348 349 350 351 352
		result.range = new types.Range(startPosition, endPosition);

		// 'inserText'-logic
		if (suggestion.snippetType === 'textmate') {
			result.insertText = new types.SnippetString(suggestion.insertText);
		} else {
			result.insertText = suggestion.insertText;
			result.textEdit = new types.TextEdit(result.range, result.insertText);
		}

		// TODO additionalEdits, command
353

354 355
		return result;
	}
B
Benjamin Pasero 已提交
356
};
357

358 359
export namespace SignatureHelp {

360 361
	export function from(signatureHelp: types.SignatureHelp): modes.SignatureHelp {
		return signatureHelp;
362 363
	}

364 365
	export function to(hints: modes.SignatureHelp): types.SignatureHelp {
		return hints;
366
	}
J
Johannes Rieken 已提交
367 368
}

J
Johannes Rieken 已提交
369 370
export namespace DocumentLink {

371
	export function from(link: vscode.DocumentLink): modes.ILink {
J
Johannes Rieken 已提交
372 373
		return {
			range: fromRange(link.range),
374
			url: link.target && link.target.toString()
J
Johannes Rieken 已提交
375 376 377
		};
	}

378
	export function to(link: modes.ILink): vscode.DocumentLink {
379
		return new types.DocumentLink(toRange(link.range), link.url && URI.parse(link.url));
J
Johannes Rieken 已提交
380 381
	}
}
382

383 384 385 386 387
export namespace TextDocumentSaveReason {

	export function to(reason: SaveReason): vscode.TextDocumentSaveReason {
		switch (reason) {
			case SaveReason.AUTO:
388
				return types.TextDocumentSaveReason.AfterDelay;
389
			case SaveReason.EXPLICIT:
390
				return types.TextDocumentSaveReason.Manual;
391 392 393 394 395
			case SaveReason.FOCUS_CHANGE:
			case SaveReason.WINDOW_CHANGE:
				return types.TextDocumentSaveReason.FocusOut;
		}
	}
396
}
397 398


399 400 401 402 403 404 405 406 407
export namespace EndOfLine {

	export function from(eol: vscode.EndOfLine): EndOfLineSequence {
		if (eol === types.EndOfLine.CRLF) {
			return EndOfLineSequence.CRLF;
		} else if (eol === types.EndOfLine.LF) {
			return EndOfLineSequence.LF;
		}
		return undefined;
408
	}
J
Johannes Rieken 已提交
409 410 411 412 413 414 415 416 417

	export function to(eol: EndOfLineSequence): vscode.EndOfLine {
		if (eol === EndOfLineSequence.CRLF) {
			return types.EndOfLine.CRLF;
		} else if (eol === EndOfLineSequence.LF) {
			return types.EndOfLine.LF;
		}
		return undefined;
	}
418 419
}

J
Johannes Rieken 已提交
420 421 422
export namespace ProgressLocation {
	export function from(loc: vscode.ProgressLocation): MainProgressLocation {
		switch (loc) {
423
			case types.ProgressLocation.SourceControl: return MainProgressLocation.Scm;
J
Johannes Rieken 已提交
424 425 426 427 428
			case types.ProgressLocation.Window: return MainProgressLocation.Window;
		}
		return undefined;
	}
}