Skip to content

Commit

Permalink
Add files.eol option and pick it up when creating models
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdima committed Feb 24, 2016
1 parent 23d0b17 commit 639a3cb
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 12 deletions.
29 changes: 29 additions & 0 deletions src/vs/editor/browser/standalone/simpleServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry';
import {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace';
import * as editorCommon from 'vs/editor/common/editorCommon';
import {ICodeEditor, IDiffEditor} from 'vs/editor/browser/editorBrowser';
import {ConfigurationService, IContent, IStat} from 'vs/platform/configuration/common/configurationService';
import URI from 'vs/base/common/uri';

export class SimpleEditor implements IEditor {

Expand Down Expand Up @@ -285,3 +287,30 @@ export class SimplePluginService extends AbstractPluginService<ActivatedPlugin>
}

}

export class SimpleConfigurationService extends ConfigurationService {

protected resolveContents(resources: URI[]): TPromise<IContent[]> {
return TPromise.as(resources.map((resource) => {
return {
resource: resource,
value: ''
};
}));
}

protected resolveContent(resource: URI): TPromise<IContent> {
return TPromise.as({
resource: resource,
value: ''
});
}

protected resolveStat(resource: URI): TPromise<IStat> {
return TPromise.as({
resource: resource,
isDirectory: false
});
}

}
12 changes: 7 additions & 5 deletions src/vs/editor/browser/standalone/standaloneServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {MainThreadModeServiceImpl} from 'vs/editor/common/services/modeServiceIm
import {IModelService} from 'vs/editor/common/services/modelService';
import {ModelServiceImpl} from 'vs/editor/common/services/modelServiceImpl';
import {CodeEditorServiceImpl} from 'vs/editor/browser/services/codeEditorServiceImpl';
import {SimpleEditorRequestService, SimpleMessageService, SimplePluginService, StandaloneKeybindingService} from 'vs/editor/browser/standalone/simpleServices';
import {SimpleConfigurationService, SimpleEditorRequestService, SimpleMessageService, SimplePluginService, StandaloneKeybindingService} from 'vs/editor/browser/standalone/simpleServices';

export interface IEditorContextViewService extends IContextViewService {
dispose(): void;
Expand Down Expand Up @@ -73,6 +73,7 @@ export interface IEditorOverrideServices {
}

export interface IStaticServices {
configurationService: IConfigurationService;
threadService: IThreadService;
modeService: IModeService;
pluginService: IPluginService;
Expand Down Expand Up @@ -155,9 +156,8 @@ export function getOrCreateStaticServices(services?: IEditorOverrideServices): I

let contextService = services.contextService;
if (!contextService) {
let workspaceUri = URI.create('inmemory', 'model', '/');
contextService = new BaseWorkspaceContextService({
resource: workspaceUri,
resource: URI.create('inmemory', 'model', '/'),
id: null,
name: null,
uid: null,
Expand All @@ -173,6 +173,8 @@ export function getOrCreateStaticServices(services?: IEditorOverrideServices): I
telemetryService = new MainTelemetryService({enableTelemetry: enableTelemetry});
}

let eventService = services.eventService || new EventService();
let configurationService = services.configurationService || new SimpleConfigurationService(contextService, eventService);

// warn the user that standaloneEdiktorTelemetryEndpint is absolete
if (flags.standaloneEditorTelemetryEndpoint) {
Expand All @@ -185,12 +187,12 @@ export function getOrCreateStaticServices(services?: IEditorOverrideServices): I
let markerService = services.markerService || new MainProcessMarkerService(threadService);
let requestService = services.requestService || new SimpleEditorRequestService(contextService, telemetryService);
let modeService = services.modeService || new MainThreadModeServiceImpl(threadService, pluginService);
let modelService = services.modelService || new ModelServiceImpl(threadService, markerService, modeService);
let modelService = services.modelService || new ModelServiceImpl(threadService, markerService, modeService, configurationService);
let editorWorkerService = services.editorWorkerService || new EditorWorkerServiceImpl(modelService);
let codeEditorService = services.codeEditorService || new CodeEditorServiceImpl();
let eventService = services.eventService || new EventService();

staticServices = {
configurationService: configurationService,
pluginService: pluginService,
modeService: modeService,
threadService: threadService,
Expand Down
31 changes: 28 additions & 3 deletions src/vs/editor/common/services/modelServiceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {IModeService} from 'vs/editor/common/services/modeService';
import {IModelService} from 'vs/editor/common/services/modelService';
import {IResourceService} from 'vs/editor/common/services/resourceService';
import * as platform from 'vs/base/common/platform';
import {IConfigurationService, ConfigurationServiceEventTypes, IConfigurationServiceEvent} from 'vs/platform/configuration/common/configuration';

export interface IRawModelData {
url:URI;
Expand Down Expand Up @@ -175,22 +176,46 @@ export class ModelServiceImpl implements IModelService {
private _markerServiceSubscription: IDisposable;
private _threadService: IThreadService;
private _modeService: IModeService;
private _configurationService: IConfigurationService;
private _configurationServiceSubscription: IDisposable;
private _workerHelper: ModelServiceWorkerHelper;

private _onModelAdded: Emitter<editorCommon.IModel>;
private _onModelRemoved: Emitter<editorCommon.IModel>;
private _onModelModeChanged: Emitter<{ model: editorCommon.IModel; oldModeId: string; }>;
private _defaultEOL: editorCommon.DefaultEndOfLine;

/**
* All the models known in the system.
*/
private _models: {[modelId:string]:ModelData;};

constructor(threadService: IThreadService, markerService: IMarkerService, modeService:IModeService) {
constructor(
threadService: IThreadService,
markerService: IMarkerService,
modeService: IModeService,
configurationService: IConfigurationService
) {
this._defaultEOL = (platform.isLinux || platform.isMacintosh) ? editorCommon.DefaultEndOfLine.LF : editorCommon.DefaultEndOfLine.CRLF;
this._threadService = threadService;
this._markerService = markerService;
this._modeService = modeService;
this._workerHelper = this._threadService.getRemotable(ModelServiceWorkerHelper);
this._configurationService = configurationService;

let readDefaultEOL = (config:any) => {
if (config.files.eol === '\r\n') {
this._defaultEOL = editorCommon.DefaultEndOfLine.CRLF;
} else if (config.files.eol === '\n') {
this._defaultEOL = editorCommon.DefaultEndOfLine.LF;
}
};
this._configurationServiceSubscription = this._configurationService.addListener2(ConfigurationServiceEventTypes.UPDATED, (e: IConfigurationServiceEvent) => {
readDefaultEOL(e.config);
});
this._configurationService.loadConfiguration().then((config) => {
readDefaultEOL(config);
});

this._models = {};

Expand All @@ -207,6 +232,7 @@ export class ModelServiceImpl implements IModelService {
if(this._markerServiceSubscription) {
this._markerServiceSubscription.dispose();
}
this._configurationServiceSubscription.dispose();
}

private _handleMarkerChange(changedResources: URI[]): void {
Expand All @@ -231,9 +257,8 @@ export class ModelServiceImpl implements IModelService {
}

private _createModelData(value:string, modeOrPromise:TPromise<IMode>|IMode, resource: URI): ModelData {
let defaultEOL = (platform.isLinux || platform.isMacintosh) ? editorCommon.DefaultEndOfLine.LF : editorCommon.DefaultEndOfLine.CRLF;
// create & save the model
let model = new Model(value, defaultEOL, modeOrPromise, resource);
let model = new Model(value, this._defaultEOL, modeOrPromise, resource);
let modelId = MODEL_ID(model.getAssociatedResource());

if (this._models[modelId]) {
Expand Down
50 changes: 47 additions & 3 deletions src/vs/editor/test/common/servicesTestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ import {ModeServiceImpl} from 'vs/editor/common/services/modeServiceImpl';
import {IModelService} from 'vs/editor/common/services/modelService';
import {ModelServiceImpl} from 'vs/editor/common/services/modelServiceImpl';
import {IResourceService} from 'vs/editor/common/services/resourceService';
import {TPromise} from 'vs/base/common/winjs.base';
import URI from 'vs/base/common/uri';
import {ConfigurationService, IContent, IStat} from 'vs/platform/configuration/common/configurationService';
import {BaseWorkspaceContextService} from 'vs/platform/workspace/common/baseWorkspaceContextService';
import {EventService} from 'vs/platform/event/common/eventService';

export interface IMockPlatformServices {
threadService?:IThreadService;
Expand Down Expand Up @@ -145,15 +150,54 @@ export function createMockModeService(): IModeService {
}

export function createMockModelService(): IModelService {
let contextService = new BaseWorkspaceContextService({
resource: URI.create('inmemory', 'model', '/'),
id: null,
name: null,
uid: null,
mtime: null
}, {});
let eventService = new EventService();
let configurationService = new MockConfigurationService(contextService, eventService);
var threadService = NULL_THREAD_SERVICE;
var pluginService = new MockPluginService();
var modeService = new MockModeService(threadService, pluginService);
var modelService = new MockModelService(threadService, null, modeService);
var modelService = new MockModelService(threadService, null, modeService, configurationService);
var inst = createInstantiationService({
threadService: threadService,
pluginService: pluginService,
modeService: modeService
modeService: modeService,
contextService: contextService,
eventService: eventService,
configurationService: configurationService
});
threadService.setInstantiationService(inst);
return modelService;
}
}

export class MockConfigurationService extends ConfigurationService {

protected resolveContents(resources: URI[]): TPromise<IContent[]> {
return TPromise.as(resources.map((resource) => {
return {
resource: resource,
value: ''
};
}));
}

protected resolveContent(resource: URI): TPromise<IContent> {
return TPromise.as({
resource: resource,
value: ''
});
}

protected resolveStat(resource: URI): TPromise<IStat> {
return TPromise.as({
resource: resource,
isDirectory: false
});
}

}
2 changes: 1 addition & 1 deletion src/vs/workbench/electron-browser/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ export class WorkbenchShell {
this.keybindingService.setPluginService(pluginService);

let modeService = new MainThreadModeServiceImpl(this.threadService, pluginService);
let modelService = new ModelServiceImpl(this.threadService, markerService, modeService);
let modelService = new ModelServiceImpl(this.threadService, markerService, modeService, configService);
let editorWorkerService = new EditorWorkerServiceImpl(modelService);

let untitledEditorService = new UntitledEditorService();
Expand Down
10 changes: 10 additions & 0 deletions src/vs/workbench/parts/files/browser/files.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {IKeybindings} from 'vs/platform/keybinding/common/keybindingService';
import {IViewletService} from 'vs/workbench/services/viewlet/common/viewletService';
import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService';
import {KeyMod, KeyCode} from 'vs/base/common/keyCodes';
import * as platform from 'vs/base/common/platform';

// Viewlet Action
export class OpenExplorerViewletAction extends ToggleViewletAction {
Expand Down Expand Up @@ -200,6 +201,15 @@ configurationRegistry.registerConfiguration({
'default': 'utf8',
'description': nls.localize('encoding', "The default character set encoding to use when reading and writing files."),
},
'files.eol': {
'type': 'string',
'enum': [
'\n',
'\r\n'
],
'default': (platform.isLinux || platform.isMacintosh) ? '\n' : '\r\n',
'description': nls.localize('eol', "The default end of line character."),
},
'files.trimTrailingWhitespace': {
'type': 'boolean',
'default': false,
Expand Down

0 comments on commit 639a3cb

Please sign in to comment.