extHostTypeConverters.ts 24.8 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

7
import * as modes from 'vs/editor/common/modes';
8
import * as types from './extHostTypes';
9 10
import { ITextEditorOptions } from 'vs/platform/editor/common/editor';
import { EditorPosition } from 'vs/workbench/api/shared/editor';
11
import { IDecorationOptions } from 'vs/editor/common/editorCommon';
A
Alex Dima 已提交
12
import { EndOfLineSequence } from 'vs/editor/common/model';
J
Johannes Rieken 已提交
13
import * as vscode from 'vscode';
14
import URI from 'vs/base/common/uri';
J
Johannes Rieken 已提交
15
import { ProgressLocation as MainProgressLocation } from 'vs/platform/progress/common/progress';
16
import { SaveReason } from 'vs/workbench/services/textfile/common/textfiles';
17 18 19
import { IPosition } from 'vs/editor/common/core/position';
import { IRange } from 'vs/editor/common/core/range';
import { ISelection } from 'vs/editor/common/core/selection';
20
import * as htmlContent from 'vs/base/common/htmlContent';
21
import { IRelativePattern } from 'vs/base/common/glob';
22
import * as languageSelector from 'vs/editor/common/modes/languageSelector';
23
import { WorkspaceEditDto, ResourceTextEditDto } from 'vs/workbench/api/node/extHost.protocol';
24
import { MarkerSeverity, IRelatedInformation, IMarkerData, MarkerTag } from 'vs/platform/markers/common/markers';
E
Erich Gamma 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

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

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

export interface SelectionLike extends RangeLike {
	anchor: PositionLike;
	active: PositionLike;
}
40
export namespace Selection {
E
Erich Gamma 已提交
41

42 43 44 45 46 47
	export function to(selection: ISelection): types.Selection {
		let { selectionStartLineNumber, selectionStartColumn, positionLineNumber, positionColumn } = selection;
		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 已提交
48

49 50 51 52 53 54 55 56 57
	export function from(selection: SelectionLike): ISelection {
		let { anchor, active } = selection;
		return {
			selectionStartLineNumber: anchor.line + 1,
			selectionStartColumn: anchor.character + 1,
			positionLineNumber: active.line + 1,
			positionColumn: active.character + 1
		};
	}
E
Erich Gamma 已提交
58
}
59
export namespace Range {
E
Erich Gamma 已提交
60

61 62 63 64 65 66 67 68 69 70 71
	export function from(range: RangeLike): IRange {
		if (!range) {
			return undefined;
		}
		let { start, end } = range;
		return {
			startLineNumber: start.line + 1,
			startColumn: start.character + 1,
			endLineNumber: end.line + 1,
			endColumn: end.character + 1
		};
J
Johannes Rieken 已提交
72
	}
E
Erich Gamma 已提交
73

74 75 76 77 78 79
	export function to(range: IRange): types.Range {
		if (!range) {
			return undefined;
		}
		let { startLineNumber, startColumn, endLineNumber, endColumn } = range;
		return new types.Range(startLineNumber - 1, startColumn - 1, endLineNumber - 1, endColumn - 1);
J
Johannes Rieken 已提交
80
	}
E
Erich Gamma 已提交
81 82
}

83 84 85 86 87 88 89
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 };
	}
90 91
}

92 93 94 95 96 97 98 99 100 101
export namespace DiagnosticTag {
	export function from(value: vscode.DiagnosticTag): MarkerTag {
		switch (value) {
			case types.DiagnosticTag.Unnecessary:
				return MarkerTag.Unnecessary;
		}
		return undefined;
	}
}

102 103 104 105 106 107 108 109
export namespace Diagnostic {
	export function from(value: vscode.Diagnostic): IMarkerData {
		return {
			...Range.from(value.range),
			message: value.message,
			source: value.source,
			code: String(value.code),
			severity: DiagnosticSeverity.from(value.severity),
110 111
			relatedInformation: value.relatedInformation && value.relatedInformation.map(DiagnosticRelatedInformation.from),
			customTags: Array.isArray(value.customTags) ? value.customTags.map(DiagnosticTag.from) : undefined,
112 113
		};
	}
114 115
}

116 117 118 119 120 121 122 123 124 125
export namespace DiagnosticRelatedInformation {
	export function from(value: types.DiagnosticRelatedInformation): IRelatedInformation {
		return {
			...Range.from(value.location.range),
			message: value.message,
			resource: value.location.uri
		};
	}
	export function to(value: IRelatedInformation): types.DiagnosticRelatedInformation {
		return new types.DiagnosticRelatedInformation(new types.Location(value.resource, Range.to(value)), value.message);
E
Erich Gamma 已提交
126 127
	}
}
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
export namespace DiagnosticSeverity {

	export function from(value: number): MarkerSeverity {
		switch (value) {
			case types.DiagnosticSeverity.Error:
				return MarkerSeverity.Error;
			case types.DiagnosticSeverity.Warning:
				return MarkerSeverity.Warning;
			case types.DiagnosticSeverity.Information:
				return MarkerSeverity.Info;
			case types.DiagnosticSeverity.Hint:
				return MarkerSeverity.Hint;
		}
		return MarkerSeverity.Error;
	}
E
Erich Gamma 已提交
143

144 145 146 147 148 149 150 151 152 153 154 155
	export function to(value: MarkerSeverity): types.DiagnosticSeverity {
		switch (value) {
			case MarkerSeverity.Info:
				return types.DiagnosticSeverity.Information;
			case MarkerSeverity.Warning:
				return types.DiagnosticSeverity.Warning;
			case MarkerSeverity.Error:
				return types.DiagnosticSeverity.Error;
			case MarkerSeverity.Hint:
				return types.DiagnosticSeverity.Hint;
		}
		return types.DiagnosticSeverity.Error;
E
Erich Gamma 已提交
156 157 158
	}
}

159 160
export namespace ViewColumn {
	export function from(column?: vscode.ViewColumn): EditorPosition {
161
		let editorColumn = 0;
162 163 164
		if (typeof column !== 'number') {
			// stick with ONE
		} else if (column === <number>types.ViewColumn.Two) {
165
			editorColumn = 1;
166
		} else if (column === <number>types.ViewColumn.Three) {
167
			editorColumn = 2;
168 169 170 171
		} else if (column === <number>types.ViewColumn.Active) {
			editorColumn = undefined;
		}
		return editorColumn;
E
Erich Gamma 已提交
172 173
	}

174 175 176 177
	export function to(position?: EditorPosition): vscode.ViewColumn {
		if (typeof position !== 'number') {
			return undefined;
		}
178
		if (position === 0) {
179
			return <number>types.ViewColumn.One;
180
		} else if (position === 1) {
181
			return <number>types.ViewColumn.Two;
182
		} else if (position === 2) {
183 184
			return <number>types.ViewColumn.Three;
		}
M
Matt Bierner 已提交
185
		return undefined;
186 187
	}
}
E
Erich Gamma 已提交
188

M
Martin Aeschlimann 已提交
189
function isDecorationOptions(something: any): something is vscode.DecorationOptions {
190
	return (typeof something.range !== 'undefined');
E
Erich Gamma 已提交
191 192
}

193
export function isDecorationOptionsArr(something: vscode.Range[] | vscode.DecorationOptions[]): something is vscode.DecorationOptions[] {
E
Erich Gamma 已提交
194 195 196
	if (something.length === 0) {
		return true;
	}
M
Martin Aeschlimann 已提交
197
	return isDecorationOptions(something[0]) ? true : false;
E
Erich Gamma 已提交
198 199
}

200 201 202 203 204 205
export namespace MarkdownString {

	export function fromMany(markup: (vscode.MarkdownString | vscode.MarkedString)[]): htmlContent.IMarkdownString[] {
		return markup.map(MarkdownString.from);
	}

J
Johannes Rieken 已提交
206 207 208 209 210 211
	interface Codeblock {
		language: string;
		value: string;
	}

	function isCodeblock(thing: any): thing is Codeblock {
J
Johannes Rieken 已提交
212
		return thing && typeof thing === 'object'
J
Johannes Rieken 已提交
213 214 215 216
			&& typeof (<Codeblock>thing).language === 'string'
			&& typeof (<Codeblock>thing).value === 'string';
	}

217
	export function from(markup: vscode.MarkdownString | vscode.MarkedString): htmlContent.IMarkdownString {
J
Johannes Rieken 已提交
218 219 220 221
		if (isCodeblock(markup)) {
			const { language, value } = markup;
			return { value: '```' + language + '\n' + value + '\n```\n' };
		} else if (htmlContent.isMarkdownString(markup)) {
222
			return markup;
J
Johannes Rieken 已提交
223
		} else if (typeof markup === 'string') {
224
			return { value: <string>markup };
225
		} else {
J
Johannes Rieken 已提交
226
			return { value: '' };
227 228
		}
	}
229 230 231 232
	export function to(value: htmlContent.IMarkdownString): vscode.MarkdownString {
		const ret = new htmlContent.MarkdownString(value.value);
		ret.isTrusted = value.isTrusted;
		return ret;
233
	}
234 235 236 237 238 239 240

	export function fromStrict(value: string | types.MarkdownString): undefined | string | htmlContent.IMarkdownString {
		if (!value) {
			return undefined;
		}
		return typeof value === 'string' ? value : MarkdownString.from(value);
	}
241 242
}

243
export function fromRangeOrRangeWithMessage(ranges: vscode.Range[] | vscode.DecorationOptions[]): IDecorationOptions[] {
M
Martin Aeschlimann 已提交
244
	if (isDecorationOptionsArr(ranges)) {
245
		return ranges.map(r => {
E
Erich Gamma 已提交
246
			return {
247
				range: Range.from(r.range),
248
				hoverMessage: Array.isArray(r.hoverMessage) ? MarkdownString.fromMany(r.hoverMessage) : r.hoverMessage && MarkdownString.from(r.hoverMessage),
249
				renderOptions: <any> /* URI vs Uri */r.renderOptions
E
Erich Gamma 已提交
250 251 252
			};
		});
	} else {
M
Martin Aeschlimann 已提交
253
		return ranges.map((r): IDecorationOptions => {
E
Erich Gamma 已提交
254
			return {
255
				range: Range.from(r)
B
Benjamin Pasero 已提交
256
			};
E
Erich Gamma 已提交
257 258 259
		});
	}
}
260

261
export const TextEdit = {
262

J
Johannes Rieken 已提交
263 264
	from(edit: vscode.TextEdit): modes.TextEdit {
		return <modes.TextEdit>{
265
			text: edit.newText,
J
Johannes Rieken 已提交
266
			eol: EndOfLine.from(edit.newEol),
267
			range: Range.from(edit.range)
B
Benjamin Pasero 已提交
268
		};
269
	},
270
	to(edit: modes.TextEdit): types.TextEdit {
271
		let result = new types.TextEdit(Range.to(edit.range), edit.text);
J
Johannes Rieken 已提交
272 273
		result.newEol = EndOfLine.to(edit.eol);
		return result;
274
	}
B
Benjamin Pasero 已提交
275
};
276

277 278
export namespace WorkspaceEdit {
	export function from(value: vscode.WorkspaceEdit): modes.WorkspaceEdit {
279
		const result: modes.WorkspaceEdit = {
280
			edits: []
281
		};
282
		for (const entry of value.entries()) {
283 284 285 286 287 288 289
			const [uri, uriOrEdits] = entry;
			if (Array.isArray(uriOrEdits)) {
				// text edits
				result.edits.push({ resource: uri, edits: uriOrEdits.map(TextEdit.from) });
			} else {
				// resource edits
				result.edits.push({ oldUri: uri, newUri: uriOrEdits });
290 291 292 293 294
			}
		}
		return result;
	}

295
	export function to(value: WorkspaceEditDto) {
296 297
		const result = new types.WorkspaceEdit();
		for (const edit of value.edits) {
298 299 300 301 302
			if (Array.isArray((<ResourceTextEditDto>edit).edits)) {
				result.set(
					URI.revive((<ResourceTextEditDto>edit).resource),
					<types.TextEdit[]>(<ResourceTextEditDto>edit).edits.map(TextEdit.to)
				);
303 304 305 306 307
				// } else {
				// 	result.renameResource(
				// 		URI.revive((<ResourceFileEditDto>edit).oldUri),
				// 		URI.revive((<ResourceFileEditDto>edit).newUri)
				// 	);
308
			}
309 310 311
		}
		return result;
	}
312 313
}

314

315 316 317
export namespace SymbolKind {

	const _fromMapping: { [kind: number]: modes.SymbolKind } = Object.create(null);
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
	_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;
341 342
	_fromMapping[types.SymbolKind.Event] = modes.SymbolKind.Event;
	_fromMapping[types.SymbolKind.Operator] = modes.SymbolKind.Operator;
343
	_fromMapping[types.SymbolKind.TypeParameter] = modes.SymbolKind.TypeParameter;
344 345

	export function from(kind: vscode.SymbolKind): modes.SymbolKind {
346
		return _fromMapping[kind] || modes.SymbolKind.Property;
347 348
	}

349 350 351 352
	export function to(kind: modes.SymbolKind): vscode.SymbolKind {
		for (let k in _fromMapping) {
			if (_fromMapping[k] === kind) {
				return Number(k);
353
			}
354 355
		}
		return types.SymbolKind.Property;
356 357 358
	}
}

359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
export namespace SymbolInformation {
	export function from(info: vscode.SymbolInformation): modes.SymbolInformation {
		return <modes.SymbolInformation>{
			name: info.name,
			kind: SymbolKind.from(info.kind),
			containerName: info.containerName,
			location: location.from(info.location)
		};
	}
	export function to(info: modes.SymbolInformation): types.SymbolInformation {
		return new types.SymbolInformation(
			info.name,
			SymbolKind.to(info.kind),
			info.containerName,
			location.to(info.location)
		);
	}
376 377
}

378
export namespace HierarchicalSymbolInformation {
379
	export function from(info: vscode.Hierarchy<vscode.SymbolInformation2>): modes.SymbolInformation {
380
		let result: modes.SymbolInformation = {
381 382 383 384 385
			name: info.parent.name,
			detail: info.parent.detail,
			location: location.from(info.parent.location),
			definingRange: Range.from(info.parent.range),
			kind: SymbolKind.from(info.parent.kind)
386 387 388 389 390 391
		};
		if (info.children) {
			result.children = info.children.map(from);
		}
		return result;
	}
392 393
	export function to(info: modes.SymbolInformation): types.Hierarchy<vscode.SymbolInformation2> {
		let result = new types.Hierarchy<vscode.SymbolInformation2>(new types.SymbolInformation2(
394 395
			info.name,
			info.detail,
J
Johannes Rieken 已提交
396
			SymbolKind.to(info.kind),
397
			Range.to(info.definingRange),
398
			location.to(info.location),
399
		));
400 401 402 403 404
		if (info.children) {
			result.children = info.children.map(to);
		}
		return result;
	}
405 406
}

407
export const location = {
J
Johannes Rieken 已提交
408
	from(value: vscode.Location): modes.Location {
409
		return {
410
			range: value.range && Range.from(value.range),
J
Johannes Rieken 已提交
411
			uri: value.uri
J
Johannes Rieken 已提交
412
		};
413
	},
414
	to(value: modes.Location): types.Location {
415
		return new types.Location(value.uri, Range.to(value.range));
416
	}
J
Johannes Rieken 已提交
417
};
418

419 420 421 422 423 424 425
export namespace Hover {
	export function from(hover: vscode.Hover): modes.Hover {
		return <modes.Hover>{
			range: Range.from(hover.range),
			contents: MarkdownString.fromMany(hover.contents)
		};
	}
426

427 428 429
	export function to(info: modes.Hover): types.Hover {
		return new types.Hover(info.contents.map(MarkdownString.to), Range.to(info.range));
	}
430
}
431 432 433 434 435 436 437 438 439 440
export namespace DocumentHighlight {
	export function from(documentHighlight: vscode.DocumentHighlight): modes.DocumentHighlight {
		return {
			range: Range.from(documentHighlight.range),
			kind: documentHighlight.kind
		};
	}
	export function to(occurrence: modes.DocumentHighlight): types.DocumentHighlight {
		return new types.DocumentHighlight(Range.to(occurrence.range), occurrence.kind);
	}
441 442
}

M
Matt Bierner 已提交
443 444 445 446 447
export namespace CompletionTriggerKind {
	export function from(kind: modes.SuggestTriggerKind) {
		switch (kind) {
			case modes.SuggestTriggerKind.TriggerCharacter:
				return types.CompletionTriggerKind.TriggerCharacter;
448 449
			case modes.SuggestTriggerKind.TriggerForIncompleteCompletions:
				return types.CompletionTriggerKind.TriggerForIncompleteCompletions;
M
Matt Bierner 已提交
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
			case modes.SuggestTriggerKind.Invoke:
			default:
				return types.CompletionTriggerKind.Invoke;
		}
	}
}

export namespace CompletionContext {
	export function from(context: modes.SuggestContext): types.CompletionContext {
		return {
			triggerKind: CompletionTriggerKind.from(context.triggerKind),
			triggerCharacter: context.triggerCharacter
		};
	}
}

J
Johannes Rieken 已提交
466 467 468 469
export const CompletionItemKind = {

	from(kind: types.CompletionItemKind): modes.SuggestionType {
		switch (kind) {
470
			case types.CompletionItemKind.Method: return 'method';
J
Johannes Rieken 已提交
471 472 473 474 475 476
			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';
477
			case types.CompletionItemKind.Struct: return 'struct';
J
Johannes Rieken 已提交
478 479 480 481
			case types.CompletionItemKind.Module: return 'module';
			case types.CompletionItemKind.Property: return 'property';
			case types.CompletionItemKind.Unit: return 'unit';
			case types.CompletionItemKind.Value: return 'value';
482
			case types.CompletionItemKind.Constant: return 'constant';
J
Johannes Rieken 已提交
483
			case types.CompletionItemKind.Enum: return 'enum';
484
			case types.CompletionItemKind.EnumMember: return 'enum-member';
J
Johannes Rieken 已提交
485 486 487 488 489 490
			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';
491
			case types.CompletionItemKind.Folder: return 'folder';
492 493
			case types.CompletionItemKind.Event: return 'event';
			case types.CompletionItemKind.Operator: return 'operator';
494
			case types.CompletionItemKind.TypeParameter: return 'type-parameter';
J
Johannes Rieken 已提交
495
		}
496
		return 'property';
J
Johannes Rieken 已提交
497 498 499 500
	},

	to(type: modes.SuggestionType): types.CompletionItemKind {
		if (!type) {
501
			return types.CompletionItemKind.Property;
J
Johannes Rieken 已提交
502 503 504 505 506 507
		} else {
			return types.CompletionItemKind[type.charAt(0).toUpperCase() + type.substr(1)];
		}
	}
};

508
export namespace Suggest {
509

510
	export function to(position: types.Position, suggestion: modes.ISuggestion): types.CompletionItem {
511
		const result = new types.CompletionItem(suggestion.label);
512
		result.insertText = suggestion.insertText;
J
Johannes Rieken 已提交
513
		result.kind = CompletionItemKind.to(suggestion.type);
514
		result.detail = suggestion.detail;
515
		result.documentation = htmlContent.isMarkdownString(suggestion.documentation) ? MarkdownString.to(suggestion.documentation) : suggestion.documentation;
516 517
		result.sortText = suggestion.sortText;
		result.filterText = suggestion.filterText;
518

519
		// 'overwrite[Before|After]'-logic
520
		let overwriteBefore = (typeof suggestion.overwriteBefore === 'number') ? suggestion.overwriteBefore : 0;
M
Martin Aeschlimann 已提交
521 522 523
		let startPosition = new types.Position(position.line, Math.max(0, position.character - overwriteBefore));
		let endPosition = position;
		if (typeof suggestion.overwriteAfter === 'number') {
524 525
			endPosition = new types.Position(position.line, position.character + suggestion.overwriteAfter);
		}
526 527 528 529 530 531 532 533 534 535 536
		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
537

538 539
		return result;
	}
540
}
541

542 543 544 545
export namespace ParameterInformation {
	export function from(info: types.ParameterInformation): modes.ParameterInformation {
		return {
			label: info.label,
546
			documentation: MarkdownString.fromStrict(info.documentation)
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
		};
	}
	export function to(info: modes.ParameterInformation): types.ParameterInformation {
		return {
			label: info.label,
			documentation: htmlContent.isMarkdownString(info.documentation) ? MarkdownString.to(info.documentation) : info.documentation
		};
	}
}

export namespace SignatureInformation {

	export function from(info: types.SignatureInformation): modes.SignatureInformation {
		return {
			label: info.label,
562
			documentation: MarkdownString.fromStrict(info.documentation),
563 564 565 566 567 568 569 570 571 572 573 574
			parameters: info.parameters && info.parameters.map(ParameterInformation.from)
		};
	}

	export function to(info: modes.SignatureInformation): types.SignatureInformation {
		return {
			label: info.label,
			documentation: htmlContent.isMarkdownString(info.documentation) ? MarkdownString.to(info.documentation) : info.documentation,
			parameters: info.parameters && info.parameters.map(ParameterInformation.to)
		};
	}
}
575

576 577
export namespace SignatureHelp {

578 579 580 581 582 583
	export function from(help: types.SignatureHelp): modes.SignatureHelp {
		return {
			activeSignature: help.activeSignature,
			activeParameter: help.activeParameter,
			signatures: help.signatures && help.signatures.map(SignatureInformation.from)
		};
584 585
	}

586 587 588 589 590 591
	export function to(help: modes.SignatureHelp): types.SignatureHelp {
		return {
			activeSignature: help.activeSignature,
			activeParameter: help.activeParameter,
			signatures: help.signatures && help.signatures.map(SignatureInformation.to)
		};
592
	}
J
Johannes Rieken 已提交
593 594
}

J
Johannes Rieken 已提交
595 596
export namespace DocumentLink {

597
	export function from(link: vscode.DocumentLink): modes.ILink {
J
Johannes Rieken 已提交
598
		return {
599
			range: Range.from(link.range),
600
			url: link.target && link.target.toString()
J
Johannes Rieken 已提交
601 602 603
		};
	}

604
	export function to(link: modes.ILink): vscode.DocumentLink {
605
		return new types.DocumentLink(Range.to(link.range), link.url && URI.parse(link.url));
J
Johannes Rieken 已提交
606 607
	}
}
608

609
export namespace ColorPresentation {
610 611 612 613 614 615 616 617 618
	export function to(colorPresentation: modes.IColorPresentation): types.ColorPresentation {
		let cp = new types.ColorPresentation(colorPresentation.label);
		if (colorPresentation.textEdit) {
			cp.textEdit = TextEdit.to(colorPresentation.textEdit);
		}
		if (colorPresentation.additionalTextEdits) {
			cp.additionalTextEdits = colorPresentation.additionalTextEdits.map(value => TextEdit.to(value));
		}
		return cp;
619 620 621 622 623 624 625 626 627 628 629
	}

	export function from(colorPresentation: vscode.ColorPresentation): modes.IColorPresentation {
		return {
			label: colorPresentation.label,
			textEdit: colorPresentation.textEdit ? TextEdit.from(colorPresentation.textEdit) : undefined,
			additionalTextEdits: colorPresentation.additionalTextEdits ? colorPresentation.additionalTextEdits.map(value => TextEdit.from(value)) : undefined
		};
	}
}

630 631 632 633 634 635 636 637 638
export namespace Color {
	export function to(c: [number, number, number, number]): types.Color {
		return new types.Color(c[0], c[1], c[2], c[3]);
	}
	export function from(color: types.Color): [number, number, number, number] {
		return [color.red, color.green, color.blue, color.alpha];
	}
}

639 640 641 642 643
export namespace TextDocumentSaveReason {

	export function to(reason: SaveReason): vscode.TextDocumentSaveReason {
		switch (reason) {
			case SaveReason.AUTO:
644
				return types.TextDocumentSaveReason.AfterDelay;
645
			case SaveReason.EXPLICIT:
646
				return types.TextDocumentSaveReason.Manual;
647 648 649 650 651
			case SaveReason.FOCUS_CHANGE:
			case SaveReason.WINDOW_CHANGE:
				return types.TextDocumentSaveReason.FocusOut;
		}
	}
652
}
653 654


655 656 657 658 659 660 661 662 663
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;
664
	}
J
Johannes Rieken 已提交
665 666 667 668 669 670 671 672 673

	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;
	}
674 675
}

J
Johannes Rieken 已提交
676 677 678
export namespace ProgressLocation {
	export function from(loc: vscode.ProgressLocation): MainProgressLocation {
		switch (loc) {
679
			case types.ProgressLocation.SourceControl: return MainProgressLocation.Scm;
J
Johannes Rieken 已提交
680
			case types.ProgressLocation.Window: return MainProgressLocation.Window;
681
			case types.ProgressLocation.Notification: return MainProgressLocation.Notification;
J
Johannes Rieken 已提交
682 683 684 685
		}
		return undefined;
	}
}
686

687
export namespace FoldingRange {
688
	export function from(r: vscode.FoldingRange): modes.FoldingRange {
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
		let range: modes.FoldingRange = { start: r.start + 1, end: r.end + 1 };
		if (r.kind) {
			range.kind = FoldingRangeKind.from(r.kind);
		}
		return range;
	}
}

export namespace FoldingRangeKind {
	export function from(kind: vscode.FoldingRangeKind | undefined): modes.FoldingRangeKind | undefined {
		if (kind) {
			switch (kind) {
				case types.FoldingRangeKind.Comment:
					return modes.FoldingRangeKind.Comment;
				case types.FoldingRangeKind.Imports:
					return modes.FoldingRangeKind.Imports;
				case types.FoldingRangeKind.Region:
					return modes.FoldingRangeKind.Region;
			}
		}
		return void 0;
710 711 712
	}
}

713
export namespace TextEditorOptions {
714

715 716 717 718 719 720 721 722
	export function from(options?: vscode.TextDocumentShowOptions): ITextEditorOptions {
		if (options) {
			return {
				pinned: typeof options.preview === 'boolean' ? !options.preview : undefined,
				preserveFocus: options.preserveFocus,
				selection: typeof options.selection === 'object' ? Range.from(options.selection) : undefined
			} as ITextEditorOptions;
		}
723

724
		return undefined;
725 726 727 728
	}

}

729
export namespace GlobPattern {
730

731 732 733 734
	export function from(pattern: vscode.GlobPattern): string | IRelativePattern {
		if (typeof pattern === 'string') {
			return pattern;
		}
735

736 737 738 739 740
		if (isRelativePattern(pattern)) {
			return new types.RelativePattern(pattern.base, pattern.pattern);
		}

		return pattern; // preserve `undefined` and `null`
741 742
	}

743 744 745 746
	function isRelativePattern(obj: any): obj is vscode.RelativePattern {
		const rp = obj as vscode.RelativePattern;
		return rp && typeof rp.base === 'string' && typeof rp.pattern === 'string';
	}
747 748
}

749
export namespace LanguageSelector {
750

751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
	export function from(selector: vscode.DocumentSelector): languageSelector.LanguageSelector {
		if (!selector) {
			return undefined;
		} else if (Array.isArray(selector)) {
			return <languageSelector.LanguageSelector>selector.map(from);
		} else if (typeof selector === 'string') {
			return selector;
		} else {
			return <languageSelector.LanguageFilter>{
				language: selector.language,
				scheme: selector.scheme,
				pattern: GlobPattern.from(selector.pattern),
				exclusive: selector.exclusive
			};
		}
766
	}
767
}