extHostTypeConverters.ts 20.3 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, ITextEditorOptions } 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';
19
import * as htmlContent from 'vs/base/common/htmlContent';
20 21
import { IRelativePattern, isRelativePattern } from 'vs/base/common/glob';
import { LanguageSelector, LanguageFilter } from 'vs/editor/common/modes/languageSelector';
E
Erich Gamma 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

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

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

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

38
export function toSelection(selection: ISelection): types.Selection {
39
	let { selectionStartLineNumber, selectionStartColumn, positionLineNumber, positionColumn } = selection;
40 41 42
	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 已提交
43 44 45
}

export function fromSelection(selection: SelectionLike): ISelection {
46
	let { anchor, active } = selection;
E
Erich Gamma 已提交
47 48 49 50 51 52 53 54 55
	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 已提交
56 57 58
	if (!range) {
		return undefined;
	}
59
	let { start, end } = range;
E
Erich Gamma 已提交
60 61 62 63 64 65 66 67
	return {
		startLineNumber: start.line + 1,
		startColumn: start.character + 1,
		endLineNumber: end.line + 1,
		endColumn: end.character + 1
	};
}

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

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

80 81
export function fromPosition(position: types.Position): IPosition {
	return { lineNumber: position.line + 1, column: position.character + 1 };
82 83
}

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

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

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

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

M
Martin Aeschlimann 已提交
140
function isDecorationOptions(something: any): something is vscode.DecorationOptions {
141
	return (typeof something.range !== 'undefined');
E
Erich Gamma 已提交
142 143
}

144
export function isDecorationOptionsArr(something: vscode.Range[] | vscode.DecorationOptions[]): something is vscode.DecorationOptions[] {
E
Erich Gamma 已提交
145 146 147
	if (something.length === 0) {
		return true;
	}
M
Martin Aeschlimann 已提交
148
	return isDecorationOptions(something[0]) ? true : false;
E
Erich Gamma 已提交
149 150
}

151 152 153 154 155 156
export namespace MarkdownString {

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

J
Johannes Rieken 已提交
157 158 159 160 161 162
	interface Codeblock {
		language: string;
		value: string;
	}

	function isCodeblock(thing: any): thing is Codeblock {
J
Johannes Rieken 已提交
163
		return thing && typeof thing === 'object'
J
Johannes Rieken 已提交
164 165 166 167
			&& typeof (<Codeblock>thing).language === 'string'
			&& typeof (<Codeblock>thing).value === 'string';
	}

168
	export function from(markup: vscode.MarkdownString | vscode.MarkedString): htmlContent.IMarkdownString {
J
Johannes Rieken 已提交
169 170 171 172
		if (isCodeblock(markup)) {
			const { language, value } = markup;
			return { value: '```' + language + '\n' + value + '\n```\n' };
		} else if (htmlContent.isMarkdownString(markup)) {
173
			return markup;
J
Johannes Rieken 已提交
174
		} else if (typeof markup === 'string') {
175
			return { value: <string>markup };
176
		} else {
J
Johannes Rieken 已提交
177
			return { value: '' };
178 179
		}
	}
180 181 182 183
	export function to(value: htmlContent.IMarkdownString): vscode.MarkdownString {
		const ret = new htmlContent.MarkdownString(value.value);
		ret.isTrusted = value.isTrusted;
		return ret;
184
	}
185 186 187 188 189 190 191

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

194
export function fromRangeOrRangeWithMessage(ranges: vscode.Range[] | vscode.DecorationOptions[]): IDecorationOptions[] {
M
Martin Aeschlimann 已提交
195
	if (isDecorationOptionsArr(ranges)) {
196
		return ranges.map(r => {
E
Erich Gamma 已提交
197 198
			return {
				range: fromRange(r.range),
199
				hoverMessage: Array.isArray(r.hoverMessage) ? MarkdownString.fromMany(r.hoverMessage) : r.hoverMessage && MarkdownString.from(r.hoverMessage),
200
				renderOptions: <any> /* URI vs Uri */r.renderOptions
E
Erich Gamma 已提交
201 202 203
			};
		});
	} else {
M
Martin Aeschlimann 已提交
204
		return ranges.map((r): IDecorationOptions => {
E
Erich Gamma 已提交
205 206
			return {
				range: fromRange(r)
B
Benjamin Pasero 已提交
207
			};
E
Erich Gamma 已提交
208 209 210
		});
	}
}
211

212
export const TextEdit = {
213

J
Johannes Rieken 已提交
214 215
	from(edit: vscode.TextEdit): modes.TextEdit {
		return <modes.TextEdit>{
216
			text: edit.newText,
J
Johannes Rieken 已提交
217
			eol: EndOfLine.from(edit.newEol),
218
			range: fromRange(edit.range)
B
Benjamin Pasero 已提交
219
		};
220
	},
J
Johannes Rieken 已提交
221 222 223 224
	to(edit: modes.TextEdit): vscode.TextEdit {
		let result = new types.TextEdit(toRange(edit.range), edit.text);
		result.newEol = EndOfLine.to(edit.eol);
		return result;
225
	}
B
Benjamin Pasero 已提交
226
};
227

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
export namespace WorkspaceEdit {
	export function from(value: vscode.WorkspaceEdit): modes.WorkspaceEdit {
		const result: modes.WorkspaceEdit = { edits: [] };
		for (let entry of value.entries()) {
			let [uri, textEdits] = entry;
			for (let textEdit of textEdits) {
				result.edits.push({
					resource: uri,
					newText: textEdit.newText,
					range: fromRange(textEdit.range)
				});
			}
		}
		return result;
	}

	export function fromTextEdits(uri: vscode.Uri, textEdits: vscode.TextEdit[]): modes.WorkspaceEdit {
		const result: modes.WorkspaceEdit = { edits: [] };
		for (let textEdit of textEdits) {
			result.edits.push({
				resource: uri,
				newText: textEdit.newText,
				range: fromRange(textEdit.range)
			});
		}
		return result;
	}
}

257

258 259 260
export namespace SymbolKind {

	const _fromMapping: { [kind: number]: modes.SymbolKind } = Object.create(null);
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
	_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;
284 285
	_fromMapping[types.SymbolKind.Event] = modes.SymbolKind.Event;
	_fromMapping[types.SymbolKind.Operator] = modes.SymbolKind.Operator;
286
	_fromMapping[types.SymbolKind.TypeParameter] = modes.SymbolKind.TypeParameter;
287 288

	export function from(kind: vscode.SymbolKind): modes.SymbolKind {
289
		return _fromMapping[kind] || modes.SymbolKind.Property;
290 291
	}

292 293 294 295
	export function to(kind: modes.SymbolKind): vscode.SymbolKind {
		for (let k in _fromMapping) {
			if (_fromMapping[k] === kind) {
				return Number(k);
296
			}
297 298
		}
		return types.SymbolKind.Property;
299 300 301
	}
}

302 303
export function fromSymbolInformation(info: vscode.SymbolInformation): modes.SymbolInformation {
	return <modes.SymbolInformation>{
304
		name: info.name,
305
		kind: SymbolKind.from(info.kind),
306
		containerName: info.containerName,
307
		location: location.from(info.location)
308 309 310
	};
}

311 312 313 314
export function toSymbolInformation(bearing: modes.SymbolInformation): types.SymbolInformation {
	return new types.SymbolInformation(
		bearing.name,
		SymbolKind.to(bearing.kind),
315
		bearing.containerName,
316
		location.to(bearing.location)
317
	);
318 319 320
}


321
export const location = {
J
Johannes Rieken 已提交
322
	from(value: vscode.Location): modes.Location {
323
		return {
J
Johannes Rieken 已提交
324
			range: value.range && fromRange(value.range),
J
Johannes Rieken 已提交
325
			uri: value.uri
J
Johannes Rieken 已提交
326
		};
327
	},
328 329
	to(value: modes.Location): types.Location {
		return new types.Location(value.uri, toRange(value.range));
330
	}
J
Johannes Rieken 已提交
331
};
332

333 334
export function fromHover(hover: vscode.Hover): modes.Hover {
	return <modes.Hover>{
335
		range: fromRange(hover.range),
336
		contents: MarkdownString.fromMany(hover.contents)
B
Benjamin Pasero 已提交
337
	};
338 339
}

340
export function toHover(info: modes.Hover): types.Hover {
341
	return new types.Hover(info.contents.map(MarkdownString.to), toRange(info.range));
342 343
}

344 345
export function toDocumentHighlight(occurrence: modes.DocumentHighlight): types.DocumentHighlight {
	return new types.DocumentHighlight(toRange(occurrence.range), occurrence.kind);
346 347
}

M
Matt Bierner 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
export namespace CompletionTriggerKind {
	export function from(kind: modes.SuggestTriggerKind) {
		switch (kind) {
			case modes.SuggestTriggerKind.TriggerCharacter:
				return types.CompletionTriggerKind.TriggerCharacter;

			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 已提交
370 371 372 373
export const CompletionItemKind = {

	from(kind: types.CompletionItemKind): modes.SuggestionType {
		switch (kind) {
374
			case types.CompletionItemKind.Method: return 'method';
J
Johannes Rieken 已提交
375 376 377 378 379 380
			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';
381
			case types.CompletionItemKind.Struct: return 'struct';
J
Johannes Rieken 已提交
382 383 384 385
			case types.CompletionItemKind.Module: return 'module';
			case types.CompletionItemKind.Property: return 'property';
			case types.CompletionItemKind.Unit: return 'unit';
			case types.CompletionItemKind.Value: return 'value';
386
			case types.CompletionItemKind.Constant: return 'constant';
J
Johannes Rieken 已提交
387
			case types.CompletionItemKind.Enum: return 'enum';
388
			case types.CompletionItemKind.EnumMember: return 'enum-member';
J
Johannes Rieken 已提交
389 390 391 392 393 394
			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';
395
			case types.CompletionItemKind.Folder: return 'folder';
396 397
			case types.CompletionItemKind.Event: return 'event';
			case types.CompletionItemKind.Operator: return 'operator';
398
			case types.CompletionItemKind.TypeParameter: return 'type-parameter';
J
Johannes Rieken 已提交
399
		}
400
		return 'property';
J
Johannes Rieken 已提交
401 402 403 404
	},

	to(type: modes.SuggestionType): types.CompletionItemKind {
		if (!type) {
405
			return types.CompletionItemKind.Property;
J
Johannes Rieken 已提交
406 407 408 409 410 411
		} else {
			return types.CompletionItemKind[type.charAt(0).toUpperCase() + type.substr(1)];
		}
	}
};

412
export namespace Suggest {
413

414
	export function to(position: types.Position, suggestion: modes.ISuggestion): types.CompletionItem {
415
		const result = new types.CompletionItem(suggestion.label);
416
		result.insertText = suggestion.insertText;
J
Johannes Rieken 已提交
417
		result.kind = CompletionItemKind.to(suggestion.type);
418
		result.detail = suggestion.detail;
419
		result.documentation = htmlContent.isMarkdownString(suggestion.documentation) ? MarkdownString.to(suggestion.documentation) : suggestion.documentation;
420 421
		result.sortText = suggestion.sortText;
		result.filterText = suggestion.filterText;
422

423
		// 'overwrite[Before|After]'-logic
424
		let overwriteBefore = (typeof suggestion.overwriteBefore === 'number') ? suggestion.overwriteBefore : 0;
M
Martin Aeschlimann 已提交
425 426 427
		let startPosition = new types.Position(position.line, Math.max(0, position.character - overwriteBefore));
		let endPosition = position;
		if (typeof suggestion.overwriteAfter === 'number') {
428 429
			endPosition = new types.Position(position.line, position.character + suggestion.overwriteAfter);
		}
430 431 432 433 434 435 436 437 438 439 440
		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
441

442 443
		return result;
	}
444
}
445

446 447 448 449
export namespace ParameterInformation {
	export function from(info: types.ParameterInformation): modes.ParameterInformation {
		return {
			label: info.label,
450
			documentation: MarkdownString.fromStrict(info.documentation)
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
		};
	}
	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,
466
			documentation: MarkdownString.fromStrict(info.documentation),
467 468 469 470 471 472 473 474 475 476 477 478
			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)
		};
	}
}
479

480 481
export namespace SignatureHelp {

482 483 484 485 486 487
	export function from(help: types.SignatureHelp): modes.SignatureHelp {
		return {
			activeSignature: help.activeSignature,
			activeParameter: help.activeParameter,
			signatures: help.signatures && help.signatures.map(SignatureInformation.from)
		};
488 489
	}

490 491 492 493 494 495
	export function to(help: modes.SignatureHelp): types.SignatureHelp {
		return {
			activeSignature: help.activeSignature,
			activeParameter: help.activeParameter,
			signatures: help.signatures && help.signatures.map(SignatureInformation.to)
		};
496
	}
J
Johannes Rieken 已提交
497 498
}

J
Johannes Rieken 已提交
499 500
export namespace DocumentLink {

501
	export function from(link: vscode.DocumentLink): modes.ILink {
J
Johannes Rieken 已提交
502 503
		return {
			range: fromRange(link.range),
504
			url: link.target && link.target.toString()
J
Johannes Rieken 已提交
505 506 507
		};
	}

508
	export function to(link: modes.ILink): vscode.DocumentLink {
509
		return new types.DocumentLink(toRange(link.range), link.url && URI.parse(link.url));
J
Johannes Rieken 已提交
510 511
	}
}
512

513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
export namespace ColorPresentation {
	export function to(colorPresentation: modes.IColorPresentation): vscode.ColorPresentation {
		return {
			label: colorPresentation.label,
			textEdit: colorPresentation.textEdit ? TextEdit.to(colorPresentation.textEdit) : undefined,
			additionalTextEdits: colorPresentation.additionalTextEdits ? colorPresentation.additionalTextEdits.map(value => TextEdit.to(value)) : undefined
		};
	}

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

531 532 533 534 535
export namespace TextDocumentSaveReason {

	export function to(reason: SaveReason): vscode.TextDocumentSaveReason {
		switch (reason) {
			case SaveReason.AUTO:
536
				return types.TextDocumentSaveReason.AfterDelay;
537
			case SaveReason.EXPLICIT:
538
				return types.TextDocumentSaveReason.Manual;
539 540 541 542 543
			case SaveReason.FOCUS_CHANGE:
			case SaveReason.WINDOW_CHANGE:
				return types.TextDocumentSaveReason.FocusOut;
		}
	}
544
}
545 546


547 548 549 550 551 552 553 554 555
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;
556
	}
J
Johannes Rieken 已提交
557 558 559 560 561 562 563 564 565

	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;
	}
566 567
}

J
Johannes Rieken 已提交
568 569 570
export namespace ProgressLocation {
	export function from(loc: vscode.ProgressLocation): MainProgressLocation {
		switch (loc) {
571
			case types.ProgressLocation.SourceControl: return MainProgressLocation.Scm;
J
Johannes Rieken 已提交
572 573 574 575 576
			case types.ProgressLocation.Window: return MainProgressLocation.Window;
		}
		return undefined;
	}
}
577 578 579 580 581 582 583 584 585 586 587

export function toTextEditorOptions(options?: vscode.TextDocumentShowOptions): ITextEditorOptions {
	if (options) {
		return {
			pinned: typeof options.preview === 'boolean' ? !options.preview : undefined,
			preserveFocus: options.preserveFocus,
			selection: typeof options.selection === 'object' ? fromRange(options.selection) : undefined
		} as ITextEditorOptions;
	}

	return undefined;
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
}

export function toGlobPattern(pattern: vscode.GlobPattern): string | IRelativePattern {
	if (typeof pattern === 'string') {
		return pattern;
	}

	if (!isRelativePattern(pattern)) {
		return undefined;
	}

	return new types.RelativePattern(pattern.base, pattern.pattern);
}

export function toLanguageSelector(selector: vscode.DocumentSelector): LanguageSelector {
	if (Array.isArray(selector)) {
		return selector.map(sel => doToLanguageSelector(sel));
	}

	return doToLanguageSelector(selector);
}

function doToLanguageSelector(selector: string | vscode.DocumentFilter): string | LanguageFilter {
	if (typeof selector === 'string') {
		return selector;
	}

	return {
		language: selector.language,
		scheme: selector.scheme,
		pattern: toGlobPattern(selector.pattern)
	};
620
}