Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Bug 423072 - Provide formatting hooks on save
Signed-off-by: Olivier Thomann <Olivier_Thomann@ca.ibm.com>
  • Loading branch information
othomann committed Jul 7, 2016
1 parent fd177fc commit eeff96e
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 100 deletions.
@@ -1,6 +1,6 @@
/*******************************************************************************
* @license
* Copyright (c) 2013 IBM Corporation and others.
* Copyright (c) 2013, 2016 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License v1.0
* (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution
Expand Down Expand Up @@ -46,6 +46,8 @@ define([
smartIndentationVisible: true,
trimTrailingWhiteSpace: false,
trimTrailingWhiteSpaceVisible: true,
formatOnSave: false,
formatOnSaveVisible: true,
tabSize: 4,
tabSizeVisible: true,
expandTab: false,
Expand Down
Expand Up @@ -219,6 +219,7 @@ define([
var inputManager = this.inputManager;
inputManager.setAutoLoadEnabled(prefs.autoLoad);
inputManager.setAutoSaveTimeout(prefs.autoSave ? prefs.autoSaveTimeout : -1);
inputManager.setFormatOnSave(prefs.formatOnSave ? prefs.formatOnSave : false);
if(this.differ) {
inputManager.setSaveDiffsEnabled(prefs.saveDiffs);
this.differ.setEnabled(this.settings.diffService);
Expand Down
34 changes: 1 addition & 33 deletions bundles/org.eclipse.orion.client.ui/web/orion/formatter.js
Expand Up @@ -42,38 +42,6 @@ define ([
}
return null;
},
/*
getServiceRefs : function(registry, contentType) {
var contentTypeService = registry.getService("orion.core.contentTypeRegistry"); //$NON-NLS-0$
function getFilteredServiceRef(registry, sReference, contentType) {
var contentTypeIds = sReference.getProperty("contentType"); //$NON-NLS-0$
return contentTypeService.isSomeExtensionOf(contentType, contentTypeIds).then(function(result) {
return result ? sReference : null;
});
}
var serviceRefs = registry.getServiceReferences("orion.edit.format"); //$NON-NLS-0$
var filteredServiceRefs = [];
for (var i=0; i < serviceRefs.length; i++) {
var serviceRef = serviceRefs[i];
if (serviceRef.getProperty("contentType")) { //$NON-NLS-0$
filteredServiceRefs.push(getFilteredServiceRef(registry, serviceRef, contentType));
}
}
// Return a promise that gives the service references that aren't null
return Deferred.all(filteredServiceRefs, function(error) {return {_error: error}; }).then(
function(serviceRefs) {
var capableServiceRefs = [];
for (var i=0; i < serviceRefs.length; i++) {
var currentServiceRef = serviceRefs[i];
if (currentServiceRef && !currentServiceRef._error) {
capableServiceRefs.push(currentServiceRef);
}
}
return capableServiceRefs;
});
},
*/
isVisible: function() {
return !!this.getFormatter();
},
Expand All @@ -83,7 +51,7 @@ define ([
if (service) {
var inputManager = this.inputManager;
var selection = this.editor.getSelection();
var context = {metadata: inputManager.getFileMetadata(), start: selection.start, end: selection.end};
var context = {start: selection.start, end: selection.end};
return service.format(this.editor.getEditorContext(), context);
}
}
Expand Down
143 changes: 78 additions & 65 deletions bundles/org.eclipse.orion.client.ui/web/orion/inputManager.js
Expand Up @@ -317,6 +317,9 @@ define([
getAutoSaveEnabled: function() {
return this._autoSaveEnabled;
},
getFormatOnSaveEnabled: function() {
return this._formatOnSaveEnabled;
},
getEditor: function() {
return this.editor;
},
Expand Down Expand Up @@ -390,80 +393,87 @@ define([

this.dispatchEvent({ type: "Saving", inputManager: this}); //$NON-NLS-0$

editor.markClean();
var contents = editor.getText();
var data = contents;
if (this._getSaveDiffsEnabled() && !this._errorSaving) {
var changes = this._getUnsavedChanges();
if (changes) {
var len = 0;
for (var i = 0; i < changes.length; i++) {
len += changes[i].text.length;
}
if (contents.length > len) {
data = {
diff: changes
};
function _save(that) {
editor.markClean();
var contents = editor.getText();
var data = contents;
if (that._getSaveDiffsEnabled() && !that._errorSaving) {
var changes = that._getUnsavedChanges();
if (changes) {
var len = 0;
for (var i = 0; i < changes.length; i++) {
len += changes[i].text.length;
}
if (contents.length > len) {
data = {
diff: changes
};
}
}
}
}
this._clearUnsavedChanges();
this._errorSaving = false;
that._clearUnsavedChanges();
that._errorSaving = false;

var etag = metadata.ETag;
var args = { "ETag" : etag }; //$NON-NLS-0$
var resource = this._parsedLocation.resource;
var def = this.fileClient.write(resource, data, args);
var progress = this.progressService;
var statusService = null;
if (this.serviceRegistry) {
statusService = this.serviceRegistry.getService("orion.page.message"); //$NON-NLS-0$
}
if (progress) {
def = progress.progress(def, i18nUtil.formatMessage(messages.savingFile, input));
}
function successHandler(result) {
if (input === that.getInput()) {
metadata.ETag = result.ETag;
editor.setInput(input, null, contents, true);
var etag = metadata.ETag;
var args = { "ETag" : etag }; //$NON-NLS-0$
var resource = that._parsedLocation.resource;
var def = that.fileClient.write(resource, data, args);
var progress = that.progressService;
var statusService = null;
if (that.serviceRegistry) {
statusService = that.serviceRegistry.getService("orion.page.message"); //$NON-NLS-0$
}
if (progress) {
def = progress.progress(def, i18nUtil.formatMessage(messages.savingFile, input));
}
that.reportStatus("");
if (failedSaving && statusService) {
statusService.setProgressResult({Message:messages.Saved, Severity:"Normal"}); //$NON-NLS-0$
function successHandler(result) {
if (input === that.getInput()) {
metadata.ETag = result.ETag;
editor.setInput(input, null, contents, true);
}
that.reportStatus("");
if (failedSaving && statusService) {
statusService.setProgressResult({Message:messages.Saved, Severity:"Normal"}); //$NON-NLS-0$
}
if (that.postSave) {
that.postSave(closing);
}
return done(result);
}
if (that.postSave) {
that.postSave(closing);
function errorHandler(error) {
that.reportStatus("");
var errorMsg = handleError(statusService, error);
mMetrics.logEvent("status", "exception", (that._autoSaveActive ? "Auto-save: " : "Save: ") + errorMsg.Message); //$NON-NLS-3$ //$NON-NLS-2$ //$NON-NLS-1$ //$NON-NLS-0$
that._errorSaving = true;
return done();
}
return done(result);
}
function errorHandler(error) {
that.reportStatus("");
var errorMsg = handleError(statusService, error);
mMetrics.logEvent("status", "exception", (that._autoSaveActive ? "Auto-save: " : "Save: ") + errorMsg.Message); //$NON-NLS-3$ //$NON-NLS-2$ //$NON-NLS-1$ //$NON-NLS-0$
that._errorSaving = true;
return done();
}
def.then(successHandler, function(error) {
// expected error - HTTP 412 Precondition Failed
// occurs when file is out of sync with the server
if (error.status === 412) {
var forceSave = window.confirm(messages.saveOutOfSync);
if (forceSave) {
// repeat save operation, but without ETag
var redef = that.fileClient.write(resource, contents);
if (progress) {
redef = progress.progress(redef, i18nUtil.formatMessage(messages.savingFile, input));
def.then(successHandler, function(error) {
// expected error - HTTP 412 Precondition Failed
// occurs when file is out of sync with the server
if (error.status === 412) {
var forceSave = window.confirm(messages.saveOutOfSync);
if (forceSave) {
// repeat save operation, but without ETag
var redef = that.fileClient.write(resource, contents);
if (progress) {
redef = progress.progress(redef, i18nUtil.formatMessage(messages.savingFile, input));
}
redef.then(successHandler, errorHandler);
} else {
return done();
}
redef.then(successHandler, errorHandler);
} else {
return done();
// unknown error
errorHandler(error);
}
} else {
// unknown error
errorHandler(error);
}
});
return metadata._savingDeferred;
});
return metadata._savingDeferred;
}

if (this.getFormatOnSaveEnabled()) {
return new mFormatter.Formatter(this.serviceRegistry, this, editor).doFormat().then(function() {return _save(this);}.bind(this));
}
return _save(this);
},
setAutoLoadEnabled: function(enabled) {
this._autoLoadEnabled = enabled;
Expand Down Expand Up @@ -494,6 +504,9 @@ define([
this._idle.setTimeout(timeout);
}
},
setFormatOnSave: function(enabled) {
this._formatOnSaveEnabled = enabled;
},
setContentType: function(contentType) {
this._contentType = contentType;
},
Expand Down
Expand Up @@ -159,6 +159,7 @@ define({//Default message bundle
"autoLoad": "Auto Load:",
"saveDiffs": "Save file as diffs:",
"trimTrailingWhiteSpace": "Trim Trailing Whitespace on Save:",
"formatOnSave": "Format code on save",
"Restore": "Restore Defaults",
"Default": "Default",
"keys": "Keys",
Expand Down
@@ -1,6 +1,6 @@
/*******************************************************************************
* @license
* Copyright (c) 2012 IBM Corporation and others.
* Copyright (c) 2012, 2016 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License v1.0
* (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution
Expand Down Expand Up @@ -112,6 +112,9 @@ define("orion/widgets/settings/EditorSettings", //$NON-NLS-0$
},
trimTrailingWhiteSpace: {
create: createBooleanProperty
},
formatOnSave: {
create: createBooleanProperty
}
},
typing: {
Expand Down

0 comments on commit eeff96e

Please sign in to comment.