json.ts 7.4 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8
/*---------------------------------------------------------------------------------------------
 *  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 EditorCommon = require('vs/editor/common/editorCommon');
import Modes = require('vs/editor/common/modes');
9
import URI from 'vs/base/common/uri';
E
Erich Gamma 已提交
10 11 12 13 14
import WinJS = require('vs/base/common/winjs.base');
import Platform = require('vs/platform/platform');
import nls = require('vs/nls');
import jsonWorker = require('vs/languages/json/common/jsonWorker');
import tokenization = require('vs/languages/json/common/features/tokenization');
15
import {AbstractMode, createWordRegExp, ModeWorkerManager} from 'vs/editor/common/modes/abstractMode';
E
Erich Gamma 已提交
16
import {OneWorkerAttr, AllWorkersAttr} from 'vs/platform/thread/common/threadService';
17
import {IThreadService, ThreadAffinity} from 'vs/platform/thread/common/thread';
M
Martin Aeschlimann 已提交
18
import {IJSONContributionRegistry, Extensions, ISchemaContributions} from 'vs/platform/jsonschemas/common/jsonContributionRegistry';
E
Erich Gamma 已提交
19
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
20
import {RichEditSupport} from 'vs/editor/common/modes/supports/richEditSupport';
21
import {SuggestSupport} from 'vs/editor/common/modes/supports/suggestSupport';
E
Erich Gamma 已提交
22

23
export class JSONMode extends AbstractMode implements Modes.IExtraInfoSupport, Modes.IOutlineSupport {
E
Erich Gamma 已提交
24 25

	public tokenizationSupport: Modes.ITokenizationSupport;
26
	public richEditSupport: Modes.IRichEditSupport;
27
	public configSupport:Modes.IConfigurationSupport;
28
	public inplaceReplaceSupport:Modes.IInplaceReplaceSupport;
E
Erich Gamma 已提交
29 30 31 32 33 34 35
	public extraInfoSupport: Modes.IExtraInfoSupport;
	public outlineSupport: Modes.IOutlineSupport;
	public formattingSupport: Modes.IFormattingSupport;
	public suggestSupport: Modes.ISuggestSupport;

	public outlineGroupLabel : { [name: string]: string; };

36
	private _modeWorkerManager: ModeWorkerManager<jsonWorker.JSONWorker>;
37
	private _threadService:IThreadService;
38

E
Erich Gamma 已提交
39 40 41 42 43
	constructor(
		descriptor:Modes.IModeDescriptor,
		@IInstantiationService instantiationService: IInstantiationService,
		@IThreadService threadService: IThreadService
	) {
44
		super(descriptor.id);
45
		this._modeWorkerManager = new ModeWorkerManager<jsonWorker.JSONWorker>(descriptor, 'vs/languages/json/common/jsonWorker', 'JSONWorker', null, instantiationService);
46
		this._threadService = threadService;
E
Erich Gamma 已提交
47 48

		this.tokenizationSupport = tokenization.createTokenizationSupport(this, true);
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

		this.richEditSupport = new RichEditSupport(this.getId(), {

			wordPattern: createWordRegExp('.-'),

			comments: {
				lineComment: '//',
				blockComment: ['/*', '*/']
			},

			brackets: [
				['{', '}'],
				['[', ']']
			],

			__electricCharacterSupport: {
				brackets: [
					{ tokenType:'delimiter.bracket.json', open: '{', close: '}', isElectric: true },
					{ tokenType:'delimiter.array.json', open: '[', close: ']', isElectric: true }
				]
			},

			__characterPairSupport: {
				autoClosingPairs: [
					{ open: '{', close: '}', notIn: ['string'] },
					{ open: '[', close: ']', notIn: ['string'] },
					{ open: '"', close: '"', notIn: ['string'] }
				]
			}
		});
E
Erich Gamma 已提交
79 80

		this.extraInfoSupport = this;
81
		this.inplaceReplaceSupport = this;
82
		this.configSupport = this;
E
Erich Gamma 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95

		// Initialize Outline support
		this.outlineSupport = this;
		this.outlineGroupLabel = Object.create(null);
		this.outlineGroupLabel['object'] = nls.localize('object', "objects");
		this.outlineGroupLabel['array'] = nls.localize('array', "arrays");
		this.outlineGroupLabel['string'] = nls.localize('string', "strings");
		this.outlineGroupLabel['number'] = nls.localize('number', "numbers");
		this.outlineGroupLabel['boolean'] = nls.localize('boolean', "booleans");
		this.outlineGroupLabel['null'] = nls.localize('undefined', "undefined");

		this.formattingSupport = this;

96
		this.suggestSupport = new SuggestSupport(this.getId(), {
E
Erich Gamma 已提交
97 98 99 100 101 102 103
			triggerCharacters: [],
			excludeTokens: ['comment.line.json', 'comment.block.json'],
			suggest: (resource, position) => this.suggest(resource, position)});
	}

	public creationDone(): void {
		if (this._threadService.isInMainThread) {
104 105 106
			// Pick a worker to do validation
			this._pickAWorkerToValidate();

E
Erich Gamma 已提交
107 108 109 110 111 112 113 114 115
			// Configure all workers
			this._configureWorkerSchemas(this.getSchemaConfiguration());
			var contributionRegistry = <IJSONContributionRegistry> Platform.Registry.as(Extensions.JSONContribution);
			contributionRegistry.addRegistryChangedListener(e => {
				this._configureWorkerSchemas(this.getSchemaConfiguration());
			});
		}
	}

116 117 118 119
	private _worker<T>(runner:(worker:jsonWorker.JSONWorker)=>WinJS.TPromise<T>): WinJS.TPromise<T> {
		return this._modeWorkerManager.worker(runner);
	}

E
Erich Gamma 已提交
120 121 122 123 124
	private getSchemaConfiguration() : ISchemaContributions {
		var contributionRegistry = <IJSONContributionRegistry> Platform.Registry.as(Extensions.JSONContribution);
		return contributionRegistry.getSchemaContributions();
	}

125 126 127 128 129 130 131 132 133 134 135 136 137
	public configure(options:any): WinJS.TPromise<void> {
		if (this._threadService.isInMainThread) {
			return this._configureWorkers(options);
		} else {
			return this._worker((w) => w._doConfigure(options));
		}
	}

	static $_configureWorkers = AllWorkersAttr(JSONMode, JSONMode.prototype._configureWorkers);
	private _configureWorkers(options:any): WinJS.TPromise<void> {
		return this._worker((w) => w._doConfigure(options));
	}

E
Erich Gamma 已提交
138 139 140 141 142
	static $_configureWorkerSchemas = AllWorkersAttr(JSONMode, JSONMode.prototype._configureWorkerSchemas);
	private _configureWorkerSchemas(data:ISchemaContributions): WinJS.TPromise<boolean> {
		return this._worker((w) => w.setSchemaContributions(data));
	}

143 144 145 146 147
	static $_pickAWorkerToValidate = OneWorkerAttr(JSONMode, JSONMode.prototype._pickAWorkerToValidate, ThreadAffinity.Group1);
	private _pickAWorkerToValidate(): WinJS.TPromise<void> {
		return this._worker((w) => w.enableValidator());
	}

148 149 150 151 152
	static $navigateValueSet = OneWorkerAttr(JSONMode, JSONMode.prototype.navigateValueSet);
	public navigateValueSet(resource:URI, position:EditorCommon.IRange, up:boolean):WinJS.TPromise<Modes.IInplaceReplaceSupportResult> {
		return this._worker((w) => w.navigateValueSet(resource, position, up));
	}

153 154 155 156 157
	static $suggest = OneWorkerAttr(JSONMode, JSONMode.prototype.suggest);
	public suggest(resource:URI, position:EditorCommon.IPosition):WinJS.TPromise<Modes.ISuggestResult[]> {
		return this._worker((w) => w.suggest(resource, position));
	}

E
Erich Gamma 已提交
158
	static $computeInfo = OneWorkerAttr(JSONMode, JSONMode.prototype.computeInfo);
159
	public computeInfo(resource:URI, position:EditorCommon.IPosition): WinJS.TPromise<Modes.IComputeExtraInfoResult> {
E
Erich Gamma 已提交
160 161 162 163
		return this._worker((w) => w.computeInfo(resource, position));
	}

	static $getOutline = OneWorkerAttr(JSONMode, JSONMode.prototype.getOutline);
164
	public getOutline(resource:URI):WinJS.TPromise<Modes.IOutlineEntry[]> {
E
Erich Gamma 已提交
165 166 167 168
		return this._worker((w) => w.getOutline(resource));
	}

	static $formatDocument = OneWorkerAttr(JSONMode, JSONMode.prototype.formatDocument);
169
	public formatDocument(resource:URI, options:Modes.IFormattingOptions):WinJS.TPromise<EditorCommon.ISingleEditOperation[]> {
E
Erich Gamma 已提交
170 171 172 173
		return this._worker((w) => w.format(resource, null, options));
	}

	static $formatRange = OneWorkerAttr(JSONMode, JSONMode.prototype.formatRange);
174
	public formatRange(resource:URI, range:EditorCommon.IRange, options:Modes.IFormattingOptions):WinJS.TPromise<EditorCommon.ISingleEditOperation[]> {
E
Erich Gamma 已提交
175 176 177
		return this._worker((w) => w.format(resource, range, options));
	}
}