Skip to content

Commit

Permalink
Clean up color theme URI usage
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdima committed Jun 13, 2018
1 parent bf79b49 commit 0afdd85
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/vs/base/common/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function basename(path: string): string {
}

/**
* @returns {{.far}} from boo.far or the empty string.
* @returns `.far` from `boo.far` or the empty string.
*/
export function extname(path: string): string {
path = basename(path);
Expand Down
46 changes: 23 additions & 23 deletions src/vs/workbench/services/themes/electron-browser/colorThemeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import { TPromise } from 'vs/base/common/winjs.base';
import * as nls from 'vs/nls';
import * as types from 'vs/base/common/types';
import * as objects from 'vs/base/common/objects';

import * as pfs from 'vs/base/node/pfs';

import * as resources from 'vs/base/common/resources';
import { Extensions, IColorRegistry, ColorIdentifier, editorBackground, editorForeground } from 'vs/platform/theme/common/colorRegistry';
import { ThemeType } from 'vs/platform/theme/common/themeService';
import { Registry } from 'vs/platform/registry/common/platform';
import { WorkbenchThemeService, IColorCustomizations } from 'vs/workbench/services/themes/electron-browser/workbenchThemeService';
import { IColorCustomizations } from 'vs/workbench/services/themes/electron-browser/workbenchThemeService';
import { getParseErrorMessage } from 'vs/base/common/jsonErrorMessages';
import URI from 'vs/base/common/uri';
import { IFileService } from 'vs/platform/files/common/files';

let colorRegistry = Registry.as<IColorRegistry>(Extensions.ColorContribution);

Expand All @@ -44,7 +44,7 @@ export class ColorThemeData implements IColorTheme {
settingsId: string;
description?: string;
isLoaded: boolean;
path?: string;
location?: URI;
extensionData: ExtensionData;

get tokenColors(): ITokenColorizationRule[] {
Expand Down Expand Up @@ -136,10 +136,10 @@ export class ColorThemeData implements IColorTheme {
}
}

public ensureLoaded(themeService: WorkbenchThemeService): TPromise<void> {
public ensureLoaded(fileService: IFileService): TPromise<void> {
if (!this.isLoaded) {
if (this.path) {
return _loadColorThemeFromFile(this.path, this.themeTokenColors, this.colorMap).then(_ => {
if (this.location) {
return _loadColorTheme(fileService, this.location, this.themeTokenColors, this.colorMap).then(_ => {
this.isLoaded = true;
this.sanitizeTokenColors();
});
Expand Down Expand Up @@ -244,7 +244,7 @@ export class ColorThemeData implements IColorTheme {
}
}

static fromExtensionTheme(theme: IThemeExtensionPoint, normalizedAbsolutePath: string, extensionData: ExtensionData): ColorThemeData {
static fromExtensionTheme(theme: IThemeExtensionPoint, colorThemeLocation: URI, extensionData: ExtensionData): ColorThemeData {
let baseTheme: string = theme['uiTheme'] || 'vs-dark';

let themeSelector = toCSSSelector(extensionData.extensionId + '-' + Paths.normalize(theme.path));
Expand All @@ -253,7 +253,7 @@ export class ColorThemeData implements IColorTheme {
themeData.label = theme.label || Paths.basename(theme.path);
themeData.settingsId = theme.id || themeData.label;
themeData.description = theme.description;
themeData.path = normalizedAbsolutePath;
themeData.location = colorThemeLocation;
themeData.extensionData = extensionData;
themeData.isLoaded = false;
return themeData;
Expand All @@ -270,17 +270,17 @@ function toCSSSelector(str: string) {
return str;
}

function _loadColorThemeFromFile(themePath: string, resultRules: ITokenColorizationRule[], resultColors: IColorMap): TPromise<any> {
if (Paths.extname(themePath) === '.json') {
return pfs.readFile(themePath).then(content => {
function _loadColorTheme(fileService: IFileService, themeLocation: URI, resultRules: ITokenColorizationRule[], resultColors: IColorMap): TPromise<any> {
if (Paths.extname(themeLocation.path) === '.json') {
return fileService.resolveContent(themeLocation).then(content => {
let errors: Json.ParseError[] = [];
let contentValue = Json.parse(content.toString(), errors);
let contentValue = Json.parse(content.value.toString(), errors);
if (errors.length > 0) {
return TPromise.wrapError(new Error(nls.localize('error.cannotparsejson', "Problems parsing JSON theme file: {0}", errors.map(e => getParseErrorMessage(e.error)).join(', '))));
}
let includeCompletes = TPromise.as(null);
if (contentValue.include) {
includeCompletes = _loadColorThemeFromFile(Paths.join(Paths.dirname(themePath), contentValue.include), resultRules, resultColors);
includeCompletes = _loadColorTheme(fileService, resources.joinPath(resources.dirname(themeLocation), contentValue.include), resultRules, resultColors);
}
return includeCompletes.then(_ => {
if (Array.isArray(contentValue.settings)) {
Expand All @@ -290,7 +290,7 @@ function _loadColorThemeFromFile(themePath: string, resultRules: ITokenColorizat
let colors = contentValue.colors;
if (colors) {
if (typeof colors !== 'object') {
return TPromise.wrapError(new Error(nls.localize({ key: 'error.invalidformat.colors', comment: ['{0} will be replaced by a path. Values in quotes should not be translated.'] }, "Problem parsing color theme file: {0}. Property 'colors' is not of type 'object'.", themePath)));
return TPromise.wrapError(new Error(nls.localize({ key: 'error.invalidformat.colors', comment: ['{0} will be replaced by a path. Values in quotes should not be translated.'] }, "Problem parsing color theme file: {0}. Property 'colors' is not of type 'object'.", themeLocation)));
}
// new JSON color themes format
for (let colorId in colors) {
Expand All @@ -306,16 +306,16 @@ function _loadColorThemeFromFile(themePath: string, resultRules: ITokenColorizat
resultRules.push(...tokenColors);
return null;
} else if (typeof tokenColors === 'string') {
return _loadSyntaxTokensFromFile(Paths.join(Paths.dirname(themePath), tokenColors), resultRules, {});
return _loadSyntaxTokens(fileService, resources.joinPath(resources.dirname(themeLocation), tokenColors), resultRules, {});
} else {
return TPromise.wrapError(new Error(nls.localize({ key: 'error.invalidformat.tokenColors', comment: ['{0} will be replaced by a path. Values in quotes should not be translated.'] }, "Problem parsing color theme file: {0}. Property 'tokenColors' should be either an array specifying colors or a path to a TextMate theme file", themePath)));
return TPromise.wrapError(new Error(nls.localize({ key: 'error.invalidformat.tokenColors', comment: ['{0} will be replaced by a path. Values in quotes should not be translated.'] }, "Problem parsing color theme file: {0}. Property 'tokenColors' should be either an array specifying colors or a path to a TextMate theme file", themeLocation)));
}
}
return null;
});
});
} else {
return _loadSyntaxTokensFromFile(themePath, resultRules, resultColors);
return _loadSyntaxTokens(fileService, themeLocation, resultRules, resultColors);
}
}

Expand All @@ -324,11 +324,11 @@ function getPListParser() {
return pListParser || import('fast-plist');
}

function _loadSyntaxTokensFromFile(themePath: string, resultRules: ITokenColorizationRule[], resultColors: IColorMap): TPromise<any> {
return pfs.readFile(themePath).then(content => {
function _loadSyntaxTokens(fileService: IFileService, themeLocation: URI, resultRules: ITokenColorizationRule[], resultColors: IColorMap): TPromise<any> {
return fileService.resolveContent(themeLocation).then(content => {
return getPListParser().then(parser => {
try {
let contentValue = parser.parse(content.toString());
let contentValue = parser.parse(content.value.toString());
let settings: ITokenColorizationRule[] = contentValue.settings;
if (!Array.isArray(settings)) {
return TPromise.wrapError(new Error(nls.localize('error.plist.invalidformat', "Problem parsing tmTheme file: {0}. 'settings' is not array.")));
Expand All @@ -340,7 +340,7 @@ function _loadSyntaxTokensFromFile(themePath: string, resultRules: ITokenColoriz
}
});
}, error => {
return TPromise.wrapError(new Error(nls.localize('error.cannotload', "Problems loading tmTheme file {0}: {1}", themePath, error.message)));
return TPromise.wrapError(new Error(nls.localize('error.cannotload', "Problems loading tmTheme file {0}: {1}", themeLocation, error.message)));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import * as nls from 'vs/nls';

import * as types from 'vs/base/common/types';
import * as Paths from 'path';
import * as resources from 'vs/base/common/resources';
import { ExtensionsRegistry, ExtensionMessageCollector } from 'vs/workbench/services/extensions/common/extensionsRegistry';
import { IColorTheme, ExtensionData, IThemeExtensionPoint, VS_LIGHT_THEME, VS_DARK_THEME, VS_HC_THEME } from 'vs/workbench/services/themes/common/workbenchThemeService';
import { ColorThemeData } from 'vs/workbench/services/themes/electron-browser/colorThemeData';
Expand Down Expand Up @@ -93,13 +93,13 @@ export class ColorThemeStore {
));
return;
}
// TODO@extensionLocation
let normalizedAbsolutePath = Paths.normalize(Paths.join(extensionLocation.fsPath, theme.path));

if (normalizedAbsolutePath.indexOf(Paths.normalize(extensionLocation.fsPath)) !== 0) {
collector.warn(nls.localize('invalid.path.1', "Expected `contributes.{0}.path` ({1}) to be included inside extension's folder ({2}). This might make the extension non-portable.", themesExtPoint.name, normalizedAbsolutePath, extensionLocation.fsPath));
const colorThemeLocation = resources.joinPath(extensionLocation, theme.path);
if (colorThemeLocation.path.indexOf(extensionLocation.path) !== 0) {
collector.warn(nls.localize('invalid.path.1', "Expected `contributes.{0}.path` ({1}) to be included inside extension's folder ({2}). This might make the extension non-portable.", themesExtPoint.name, colorThemeLocation.path, extensionLocation.path));
}
let themeData = ColorThemeData.fromExtensionTheme(theme, normalizedAbsolutePath, extensionData);

let themeData = ColorThemeData.fromExtensionTheme(theme, colorThemeLocation, extensionData);
if (themeData.id === this.extensionsColorThemes[0].id) {
this.extensionsColorThemes[0] = themeData;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ export class FileIconThemeData implements IFileIconTheme {
return TPromise.as(this.styleSheetContent);
}

static fromExtensionTheme(iconTheme: IThemeExtensionPoint, location: URI, extensionData: ExtensionData): FileIconThemeData {
static fromExtensionTheme(iconTheme: IThemeExtensionPoint, iconThemeLocation: URI, extensionData: ExtensionData): FileIconThemeData {
let themeData = new FileIconThemeData();
themeData.id = extensionData.extensionId + '-' + iconTheme.id;
themeData.label = iconTheme.label || Paths.basename(iconTheme.path);
themeData.settingsId = iconTheme.id;
themeData.description = iconTheme.description;
themeData.location = location;
themeData.location = iconThemeLocation;
themeData.extensionData = extensionData;
themeData.isLoaded = false;
return themeData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import * as nls from 'vs/nls';

import * as types from 'vs/base/common/types';
import * as Paths from 'path';
import * as resources from 'vs/base/common/resources';
import { ExtensionsRegistry, ExtensionMessageCollector } from 'vs/workbench/services/extensions/common/extensionsRegistry';
import { ExtensionData, IThemeExtensionPoint } from 'vs/workbench/services/themes/common/workbenchThemeService';
Expand Down Expand Up @@ -98,7 +97,7 @@ export class FileIconThemeStore {
}

const iconThemeLocation = resources.joinPath(extensionLocation, iconTheme.path);
if (iconThemeLocation.path.indexOf(Paths.normalize(extensionLocation.path)) !== 0) {
if (iconThemeLocation.path.indexOf(extensionLocation.path) !== 0) {
collector.warn(nls.localize('invalid.path.1', "Expected `contributes.{0}.path` ({1}) to be included inside extension's folder ({2}). This might make the extension non-portable.", iconThemeExtPoint.name, iconThemeLocation.path, extensionLocation.path));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ export class WorkbenchThemeService implements IWorkbenchThemeService {

return this.colorThemeStore.findThemeData(themeId, DEFAULT_THEME_ID).then(themeData => {
if (themeData) {
return themeData.ensureLoaded(this).then(_ => {
return themeData.ensureLoaded(this.fileService).then(_ => {
if (themeId === this.currentColorTheme.id && !this.currentColorTheme.isLoaded && this.currentColorTheme.hasEqualData(themeData)) {
// the loaded theme is identical to the perisisted theme. Don't need to send an event.
this.currentColorTheme = themeData;
Expand All @@ -298,7 +298,7 @@ export class WorkbenchThemeService implements IWorkbenchThemeService {
this.updateDynamicCSSRules(themeData);
return this.applyTheme(themeData, settingsTarget);
}, error => {
return TPromise.wrapError<IColorTheme>(new Error(nls.localize('error.cannotloadtheme', "Unable to load {0}: {1}", themeData.path, error.message)));
return TPromise.wrapError<IColorTheme>(new Error(nls.localize('error.cannotloadtheme', "Unable to load {0}: {1}", themeData.location, error.message)));
});
}
return null;
Expand Down

0 comments on commit 0afdd85

Please sign in to comment.