walkThroughContentProvider.ts 3.9 KB
Newer Older
C
Christof Marti 已提交
1 2 3 4 5 6 7 8 9
/*---------------------------------------------------------------------------------------------
 *  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 { TPromise } from 'vs/base/common/winjs.base';
import URI from 'vs/base/common/uri';
10
import { ITextModelService, ITextModelContentProvider } from 'vs/editor/common/services/resolverService';
C
Christof Marti 已提交
11 12
import { IModelService } from 'vs/editor/common/services/modelService';
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
13
import { IModel } from 'vs/editor/common/editorCommon';
C
Christof Marti 已提交
14 15
import { IModeService } from 'vs/editor/common/services/modeService';
import { IWorkbenchContribution } from 'vs/workbench/common/contributions';
16
import { marked } from 'vs/base/common/marked/marked';
17
import { Schemas } from 'vs/base/common/network';
A
Alex Dima 已提交
18
import { IRawTextSource } from 'vs/editor/common/model/textSource';
C
Christof Marti 已提交
19 20 21 22

export class WalkThroughContentProvider implements ITextModelContentProvider, IWorkbenchContribution {

	constructor(
23
		@ITextModelService private textModelResolverService: ITextModelService,
C
Christof Marti 已提交
24 25 26 27
		@ITextFileService private textFileService: ITextFileService,
		@IModeService private modeService: IModeService,
		@IModelService private modelService: IModelService,
	) {
28
		this.textModelResolverService.registerTextModelContentProvider(Schemas.walkThrough, this);
C
Christof Marti 已提交
29 30 31
	}

	public provideTextContent(resource: URI): TPromise<IModel> {
32
		const query = resource.query ? JSON.parse(resource.query) : {};
A
Alex Dima 已提交
33
		const content: TPromise<string | IRawTextSource> = (query.moduleId ? new TPromise<string>((resolve, reject) => {
34
			require([query.moduleId], content => {
35 36 37 38 39 40 41 42
				try {
					resolve(content.default());
				} catch (err) {
					reject(err);
				}
			});
		}) : this.textFileService.resolveTextContent(URI.file(resource.fsPath)).then(content => content.value));
		return content.then(content => {
C
Christof Marti 已提交
43 44
			let codeEditorModel = this.modelService.getModel(resource);
			if (!codeEditorModel) {
45
				codeEditorModel = this.modelService.createModel(content, this.modeService.getOrCreateModeByFilenameOrFirstLine(resource.fsPath), resource);
C
Christof Marti 已提交
46
			} else {
47
				this.modelService.updateModel(codeEditorModel, content);
C
Christof Marti 已提交
48 49 50 51 52 53 54 55 56
			}

			return codeEditorModel;
		});
	}

	public getId(): string {
		return 'vs.walkThroughContentProvider';
	}
57 58 59 60 61
}

export class WalkThroughSnippetContentProvider implements ITextModelContentProvider, IWorkbenchContribution {

	constructor(
62
		@ITextModelService private textModelResolverService: ITextModelService,
63 64 65 66
		@ITextFileService private textFileService: ITextFileService,
		@IModeService private modeService: IModeService,
		@IModelService private modelService: IModelService,
	) {
67
		this.textModelResolverService.registerTextModelContentProvider(Schemas.walkThroughSnippet, this);
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
	}

	public provideTextContent(resource: URI): TPromise<IModel> {
		return this.textFileService.resolveTextContent(URI.file(resource.fsPath)).then(content => {
			let codeEditorModel = this.modelService.getModel(resource);
			if (!codeEditorModel) {
				const j = parseInt(resource.fragment);

				let codeSnippet = '';
				let languageName = '';
				let i = 0;
				const renderer = new marked.Renderer();
				renderer.code = (code, lang) => {
					if (i++ === j) {
						codeSnippet = code;
						languageName = lang;
					}
					return '';
				};

88
				const markdown = content.value.lines.join('\n');
89 90 91 92 93 94
				marked(markdown, { renderer });

				const modeId = this.modeService.getModeIdForLanguageName(languageName);
				const mode = this.modeService.getOrCreateMode(modeId);
				codeEditorModel = this.modelService.createModel(codeSnippet, mode, resource);
			} else {
95
				this.modelService.updateModel(codeEditorModel, content.value);
96 97 98 99 100 101 102 103 104
			}

			return codeEditorModel;
		});
	}

	public getId(): string {
		return 'vs.walkThroughSnippetContentProvider';
	}
105
}