Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editor model creation options do not honour folder scope yet #32695

Merged
merged 2 commits into from
Aug 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/vs/editor/common/services/modelService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface IModelService {

getModels(): IModel[];

getCreationOptions(language: string): ITextModelCreationOptions;
getCreationOptions(language: string, resource: URI): ITextModelCreationOptions;

getModel(resource: URI): IModel;

Expand Down
36 changes: 20 additions & 16 deletions src/vs/editor/common/services/modelServiceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ export class ModelServiceImpl implements IModelService {
private _onModelRemoved: Emitter<editorCommon.IModel>;
private _onModelModeChanged: Emitter<{ model: editorCommon.IModel; oldModeId: string; }>;

private _modelCreationOptionsByLanguage: {
[language: string]: editorCommon.ITextModelCreationOptions;
private _modelCreationOptionsByLanguageAndResource: {
[languageAndResource: string]: editorCommon.ITextModelCreationOptions;
};

/**
Expand All @@ -210,7 +210,7 @@ export class ModelServiceImpl implements IModelService {
this._markerService = markerService;
this._configurationService = configurationService;
this._models = {};
this._modelCreationOptionsByLanguage = Object.create(null);
this._modelCreationOptionsByLanguageAndResource = Object.create(null);
this._onModelAdded = new Emitter<editorCommon.IModel>();
this._onModelRemoved = new Emitter<editorCommon.IModel>();
this._onModelModeChanged = new Emitter<{ model: editorCommon.IModel; oldModeId: string; }>();
Expand Down Expand Up @@ -238,7 +238,7 @@ export class ModelServiceImpl implements IModelService {
}

let newDefaultEOL = DEFAULT_EOL;
const eol = config.files && config.files.eol; // TODO@Sandeep (https://github.com/Microsoft/vscode/issues/29119)
const eol = config.files && config.files.eol;
if (eol === '\r\n') {
newDefaultEOL = editorCommon.DefaultEndOfLine.CRLF;
} else if (eol === '\n') {
Expand All @@ -264,27 +264,28 @@ export class ModelServiceImpl implements IModelService {
};
}

public getCreationOptions(language: string): editorCommon.ITextModelCreationOptions {
let creationOptions = this._modelCreationOptionsByLanguage[language];
public getCreationOptions(language: string, resource: URI): editorCommon.ITextModelCreationOptions {
let creationOptions = this._modelCreationOptionsByLanguageAndResource[language + resource];
if (!creationOptions) {
creationOptions = ModelServiceImpl._readModelOptions(this._configurationService.getConfiguration(null, { overrideIdentifier: language }));
this._modelCreationOptionsByLanguage[language] = creationOptions;
creationOptions = ModelServiceImpl._readModelOptions(this._configurationService.getConfiguration(null, { overrideIdentifier: language, resource }));
this._modelCreationOptionsByLanguageAndResource[language + resource] = creationOptions;
}
return creationOptions;
}

private _updateModelOptions(): void {
let oldOptionsByLanguage = this._modelCreationOptionsByLanguage;
this._modelCreationOptionsByLanguage = Object.create(null);
let oldOptionsByLanguageAndResource = this._modelCreationOptionsByLanguageAndResource;
this._modelCreationOptionsByLanguageAndResource = Object.create(null);

// Update options on all models
let keys = Object.keys(this._models);
for (let i = 0, len = keys.length; i < len; i++) {
let modelId = keys[i];
let modelData = this._models[modelId];
const language = modelData.model.getLanguageIdentifier().language;
const oldOptions = oldOptionsByLanguage[language];
const newOptions = this.getCreationOptions(language);
const uri = modelData.model.uri;
const oldOptions = oldOptionsByLanguageAndResource[language + uri];
const newOptions = this.getCreationOptions(language, uri);
ModelServiceImpl._setModelOptionsForModel(modelData.model, newOptions, oldOptions);
}
}
Expand Down Expand Up @@ -341,13 +342,16 @@ export class ModelServiceImpl implements IModelService {
this._markerService.read({ resource: model.uri }).map(marker => marker.owner).forEach(owner => this._markerService.remove(owner, [model.uri]));
}
}

// clean up cache
delete this._modelCreationOptionsByLanguageAndResource[model.getLanguageIdentifier().language + model.uri];
}

// --- begin IModelService

private _createModelData(value: string | IRawTextSource, languageIdentifier: LanguageIdentifier, resource: URI): ModelData {
// create & save the model
const options = this.getCreationOptions(languageIdentifier.language);
const options = this.getCreationOptions(languageIdentifier.language, resource);
const rawTextSource = (typeof value === 'string' ? RawTextSource.fromString(value) : value);
let model: Model = new Model(rawTextSource, options, languageIdentifier, resource);
let modelId = MODEL_ID(model.uri);
Expand All @@ -364,7 +368,7 @@ export class ModelServiceImpl implements IModelService {
}

public updateModel(model: editorCommon.IModel, value: string | IRawTextSource): void {
let options = this.getCreationOptions(model.getLanguageIdentifier().language);
let options = this.getCreationOptions(model.getLanguageIdentifier().language, model.uri);
const textSource = TextSource.create(value, options.defaultEOL);

// Return early if the text is already set in that form
Expand Down Expand Up @@ -485,8 +489,8 @@ export class ModelServiceImpl implements IModelService {
const model = modelData.model;
const oldModeId = (<textModelEvents.IModelLanguageChangedEvent>e.data).oldLanguage;
const newModeId = model.getLanguageIdentifier().language;
const oldOptions = this.getCreationOptions(oldModeId);
const newOptions = this.getCreationOptions(newModeId);
const oldOptions = this.getCreationOptions(oldModeId, model.uri);
const newOptions = this.getCreationOptions(newModeId, model.uri);
ModelServiceImpl._setModelOptionsForModel(model, newOptions, oldOptions);
this._onModelModeChanged.fire({ model, oldModeId });
}
Expand Down
4 changes: 2 additions & 2 deletions src/vs/editor/contrib/indentation/common/indentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export class ChangeIndentationSizeAction extends EditorAction {
return undefined;
}

let creationOpts = modelService.getCreationOptions(model.getLanguageIdentifier().language);
let creationOpts = modelService.getCreationOptions(model.getLanguageIdentifier().language, model.uri);
const picks = [1, 2, 3, 4, 5, 6, 7, 8].map(n => ({
id: n.toString(),
label: n.toString(),
Expand Down Expand Up @@ -300,7 +300,7 @@ export class DetectIndentation extends EditorAction {
return;
}

let creationOpts = modelService.getCreationOptions(model.getLanguageIdentifier().language);
let creationOpts = modelService.getCreationOptions(model.getLanguageIdentifier().language, model.uri);
model.detectIndentation(creationOpts.insertSpaces, creationOpts.tabSize);
}
}
Expand Down
38 changes: 38 additions & 0 deletions src/vs/editor/test/common/services/modelService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*---------------------------------------------------------------------------------------------
* 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 * as assert from 'assert';
import { TestConfigurationService } from 'vs/platform/configuration/test/common/testConfigurationService';
import { ModelServiceImpl } from 'vs/editor/common/services/modelServiceImpl';
import URI from 'vs/base/common/uri';
import * as platform from 'vs/base/common/platform';
import { DefaultEndOfLine } from "vs/editor/common/editorCommon";

suite('ModelService', () => {
let modelService: ModelServiceImpl;

setup(() => {
const configService = new TestConfigurationService();
configService.setUserConfiguration('files', { 'eol': '\n' });
configService.setUserConfiguration('files', { 'eol': '\r\n' }, URI.file(platform.isWindows ? 'c:\\myroot' : '/myroot'));

modelService = new ModelServiceImpl(null, configService);
});

teardown(() => {
modelService.dispose();
});

test('EOL setting respected depending on root', () => {
const model1 = modelService.createModel('farboo', null, null);
const model2 = modelService.createModel('farboo', null, URI.file(platform.isWindows ? 'c:\\myroot\\myfile.txt' : '/myroot/myfile.txt'));
const model3 = modelService.createModel('farboo', null, URI.file(platform.isWindows ? 'c:\\other\\myfile.txt' : '/other/myfile.txt'));

assert.equal(model1.getOptions().defaultEOL, DefaultEndOfLine.LF);
assert.equal(model2.getOptions().defaultEOL, DefaultEndOfLine.CRLF);
assert.equal(model3.getOptions().defaultEOL, DefaultEndOfLine.LF);
});
});
2 changes: 1 addition & 1 deletion src/vs/workbench/api/electron-browser/mainThreadEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export class MainThreadTextEditor {
if (newConfiguration.tabSize === 'auto' || newConfiguration.insertSpaces === 'auto') {
// one of the options was set to 'auto' => detect indentation

let creationOpts = this._modelService.getCreationOptions(this._model.getLanguageIdentifier().language);
let creationOpts = this._modelService.getCreationOptions(this._model.getLanguageIdentifier().language, this._model.uri);
let insertSpaces = creationOpts.insertSpaces;
let tabSize = creationOpts.tabSize;

Expand Down
1 change: 1 addition & 0 deletions src/vs/workbench/parts/files/browser/files.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ configurationRegistry.registerConfiguration({
],
'default': (platform.isLinux || platform.isMacintosh) ? '\n' : '\r\n',
'description': nls.localize('eol', "The default end of line character. Use \\n for LF and \\r\\n for CRLF."),
'scope': ConfigurationScope.RESOURCE
},
'files.trimTrailingWhitespace': {
'type': 'boolean',
Expand Down