|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +import * as nls from 'vs/nls'; |
| 9 | +import {IDisposable, dispose} from 'vs/base/common/lifecycle'; |
| 10 | +import {TPromise} from 'vs/base/common/winjs.base'; |
| 11 | +import {Range} from 'vs/editor/common/core/range'; |
| 12 | +import {SyncActionDescriptor} from 'vs/platform/actions/common/actions'; |
| 13 | +import {Action} from 'vs/base/common/actions'; |
| 14 | +import WorkbenchContributions = require('vs/workbench/common/contributions'); |
| 15 | +import {IWorkspace} from 'vs/platform/workspace/common/workspace'; |
| 16 | +import paths = require('vs/base/common/paths'); |
| 17 | +import URI from 'vs/base/common/uri'; |
| 18 | + |
| 19 | +import Platform = require('vs/platform/platform'); |
| 20 | +import WorkbenchActionRegistry = require('vs/workbench/common/actionRegistry'); |
| 21 | +import {IFileStat} from 'vs/platform/files/common/files'; |
| 22 | +import {TextModelWithTokens} from 'vs/editor/common/model/textModelWithTokens'; |
| 23 | +import {TextModel} from 'vs/editor/common/model/textModel'; |
| 24 | +import {IModeService} from 'vs/editor/common/services/modeService'; |
| 25 | +import pfs = require('vs/base/node/pfs'); |
| 26 | + |
| 27 | + |
| 28 | +interface Data { |
| 29 | + c: string; // content |
| 30 | + t: string; // token |
| 31 | + r: string; // rule |
| 32 | +} |
| 33 | + |
| 34 | +const ID = 'workbench.action.snapshotAction'; |
| 35 | +const LABEL = nls.localize('togglePosition', "Take Theme Snapshot"); |
| 36 | + |
| 37 | +class SnapshotAction extends Action { |
| 38 | + |
| 39 | + private currentSrcFolder: string; |
| 40 | + |
| 41 | + constructor(id: string, label: string, |
| 42 | + @IModeService private modeService: IModeService |
| 43 | + ) { |
| 44 | + super(id, label); |
| 45 | + let outFolder = require.toUrl(''); |
| 46 | + this.currentSrcFolder = paths.normalize(paths.join(outFolder, "../src/vs/workbench/parts/themes/test/electron-browser")); |
| 47 | + } |
| 48 | + |
| 49 | + public run(): TPromise<any> { |
| 50 | + let fixturesPath = URI.parse(paths.join(this.currentSrcFolder, 'fixtures')).fsPath; |
| 51 | + return pfs.readdir(fixturesPath).then(fileNames => { |
| 52 | + return TPromise.join(fileNames.map(fileName => { |
| 53 | + return pfs.readFile(paths.join(fixturesPath, fileName)).then(content => { |
| 54 | + return this.snap(fileName, content.toString()).then(result => { |
| 55 | + return this.verify(fileName, result); |
| 56 | + }); |
| 57 | + }); |
| 58 | + })); |
| 59 | + }, err => { |
| 60 | + console.log(err.toString()); |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + public getId() : string { |
| 65 | + return "TokenizationSnapshotController"; |
| 66 | + } |
| 67 | + |
| 68 | + private getEditorNode() : Element { |
| 69 | + let editorNodes = document.getElementsByClassName('monaco-editor'); |
| 70 | + if (editorNodes.length > 0) { |
| 71 | + return editorNodes.item(0); |
| 72 | + } |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + private getStyle(scope: string) : string { |
| 77 | + |
| 78 | + let element = document.createElement('span'); |
| 79 | + element.className = scope; |
| 80 | + element.hidden = true; |
| 81 | + |
| 82 | + let cssStyles = window.getComputedStyle(element); |
| 83 | + if (cssStyles) { |
| 84 | + return cssStyles.color; |
| 85 | + } |
| 86 | + return ''; |
| 87 | + } |
| 88 | + |
| 89 | + private getMatchedCSSRule(scope: string) : string { |
| 90 | + let element = document.createElement('span'); |
| 91 | + element.className = 'token ' + scope; |
| 92 | + element.hidden = true; |
| 93 | + |
| 94 | + let editorNode = this.getEditorNode(); |
| 95 | + editorNode.appendChild(element); |
| 96 | + |
| 97 | + let rulesList = window.getMatchedCSSRules(element); |
| 98 | + |
| 99 | + editorNode.removeChild(element); |
| 100 | + |
| 101 | + if (rulesList) { |
| 102 | + for (let i = rulesList.length - 1; i >= 0 ; i--) { |
| 103 | + let selectorText = <string> rulesList.item(i)['selectorText']; |
| 104 | + if (selectorText && selectorText.indexOf('.monaco-editor.vs') === 0) { |
| 105 | + return selectorText.substr(14); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return ''; |
| 111 | + } |
| 112 | + |
| 113 | + public snap(fileName: string, content: string) : TPromise<Data[]> { |
| 114 | + return this.modeService.getOrCreateModeByFilenameOrFirstLine(fileName).then(mode => { |
| 115 | + let result : Data[] = []; |
| 116 | + let model = new TextModelWithTokens([], TextModel.toRawText(content, TextModel.DEFAULT_CREATION_OPTIONS), false, mode); |
| 117 | + model.tokenIterator({lineNumber: 1, column: 1}, iterator => { |
| 118 | + while (iterator.hasNext()) { |
| 119 | + let tokenInfo = iterator.next(); |
| 120 | + let lineNumber = tokenInfo.lineNumber; |
| 121 | + let content = model.getValueInRange({ startLineNumber: lineNumber, endLineNumber: lineNumber, startColumn: tokenInfo.startColumn, endColumn: tokenInfo.endColumn}); |
| 122 | + result.push({ |
| 123 | + c: content, |
| 124 | + t: tokenInfo.token.type, |
| 125 | + r: this.getMatchedCSSRule(tokenInfo.token.type) |
| 126 | + }); |
| 127 | + } |
| 128 | + }); |
| 129 | + return result; |
| 130 | + }); |
| 131 | + } |
| 132 | + |
| 133 | + public verify(fileName: string, data: Data[]) : TPromise<any> { |
| 134 | + let dataString = JSON.stringify(data, null, '\t'); |
| 135 | + let resultFileName = fileName.replace('.', '_') + '.json'; |
| 136 | + let resultPath = URI.parse(paths.join(this.currentSrcFolder, 'results', resultFileName)).fsPath; |
| 137 | + |
| 138 | + return pfs.fileExists(resultPath).then(success => { |
| 139 | + if (success) { |
| 140 | + return pfs.readFile(resultPath).then(content => { |
| 141 | + let previousDataString = content.toString(); |
| 142 | + if (previousDataString !== dataString) { |
| 143 | + let errorResultFileName = fileName.replace('.', '_') + '.error.json'; |
| 144 | + let errorResultPath = URI.parse(paths.join(this.currentSrcFolder, 'results', errorResultFileName)).fsPath; |
| 145 | + console.log(`Different result for ${fileName}`); |
| 146 | + return pfs.writeFile(errorResultPath, dataString); |
| 147 | + } |
| 148 | + return true; |
| 149 | + }); |
| 150 | + } else { |
| 151 | + return pfs.writeFile(resultPath, dataString); |
| 152 | + } |
| 153 | + }); |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +var workbenchActionsRegistry = <WorkbenchActionRegistry.IWorkbenchActionRegistry> Platform.Registry.as(WorkbenchActionRegistry.Extensions.WorkbenchActions); |
| 158 | + |
| 159 | +workbenchActionsRegistry.registerWorkbenchAction(new SyncActionDescriptor(SnapshotAction, ID, LABEL), nls.localize('view', "View")); |
| 160 | + |
0 commit comments