Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

adding lineEndings UI in statusbar for documents #13152

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
28 changes: 26 additions & 2 deletions src/document/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ define(function (require, exports, module) {
*/
Document.prototype.isDirty = false;

/**
* Whether this document has unsaved lineEndings changes or not.
* @type {boolean}
*/
Document.prototype._lineEndingsDirty = false;

/**
* Whether this document is currently being saved.
* @type {boolean}
Expand Down Expand Up @@ -474,8 +480,7 @@ define(function (require, exports, module) {
if (!this._refreshInProgress) {
// Sync isDirty from CodeMirror state
var wasDirty = this.isDirty;
this.isDirty = !editor._codeMirror.isClean();

this.isDirty = !editor._codeMirror.isClean() || this._lineEndingsDirty;
// Notify if isDirty just changed (this also auto-adds us to working set if needed)
if (wasDirty !== this.isDirty) {
exports.trigger("_dirtyFlagChange", this);
Expand All @@ -491,6 +496,7 @@ define(function (require, exports, module) {
*/
Document.prototype._markClean = function () {
this.isDirty = false;
this._lineEndingsDirty = false;
if (this._masterEditor) {
this._masterEditor._codeMirror.markClean();
}
Expand Down Expand Up @@ -758,6 +764,24 @@ define(function (require, exports, module) {
return this.file instanceof InMemoryFile;
};

/**
* Set lineEnding of the document. Change Text in the process.
*/
Document.prototype.setLineEndings = function (lineEndings) {
this._lineEndings = lineEndings;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is toggled twice, the document is effectively in unedited state and hence should not throw a save/don't save prompt while closing the file.

Other thing is should the change of line ending be part of undo stack?

Copy link
Collaborator

@ficristo ficristo Jun 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tryed Notepad++ with a txt file. When you change the line ending, the first time goes in edited state, if you change back to the previous line ending it remains in an edited state.
So, to me, the prompt is expected.

As stated above, the line ending changes should be part of the history but requires to add custom entry in CodeMirror. If someone at Adobe could do the courtesy it would be great.

this._lineEndingsDirty = true;
this.isDirty = true;
};

/**
* Get the current document LineEnding.
*
* @return {string} - returns the current line endings of the document.
*/
Document.prototype.getLineEndings = function () {
return this._lineEndings;
};

// We dispatch events from the module level, and the instance level. Instance events are wired up
// in the Document constructor.
EventDispatcher.makeEventDispatcher(exports);
Expand Down
34 changes: 33 additions & 1 deletion src/editor/EditorStatusBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ define(function (require, exports, module) {
AppInit = require("utils/AppInit"),
DropdownButton = require("widgets/DropdownButton").DropdownButton,
EditorManager = require("editor/EditorManager"),
FileUtils = require("file/FileUtils"),
MainViewManager = require("view/MainViewManager"),
Editor = require("editor/Editor").Editor,
KeyEvent = require("utils/KeyEvent"),
Expand All @@ -49,7 +50,8 @@ define(function (require, exports, module) {
$indentType,
$indentWidthLabel,
$indentWidthInput,
$statusOverwrite;
$statusOverwrite,
$statusLineEndings;

/** Special list item for the 'set as default' gesture in language switcher dropdown */
var LANGUAGE_SET_AS_DEFAULT = {};
Expand Down Expand Up @@ -231,6 +233,23 @@ define(function (require, exports, module) {
editor.toggleOverwrite(newstate);
}

/**
* Update LineEndings indicator
* @param {Event} event (unused)
*/
function _toggleLineEndings(event) {
var editor = EditorManager.getActiveEditor(),
document = editor.document,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assignment to a window global, change to doc or similar

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assignment to window global document, change to doc or similar.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, we should really make ESLint catch this.

oldLineEndings = document.getLineEndings(),
newLineEndings = oldLineEndings === FileUtils.LINE_ENDINGS_LF ?
FileUtils.LINE_ENDINGS_CRLF :
FileUtils.LINE_ENDINGS_LF;

// update the line endings in document & status bar
document.setLineEndings(newLineEndings);
$statusLineEndings.text(newLineEndings);
}

/**
* Initialize insert/overwrite indicator
* @param {Editor} currentEditor Current editor
Expand All @@ -240,6 +259,15 @@ define(function (require, exports, module) {
$statusOverwrite.attr("title", Strings.STATUSBAR_INSOVR_TOOLTIP);
}

/**
* Initialize line endings indicator
* @param {Editor} currentEditor Current editor
*/
function _initLineEndings(currentEditor) {
$statusLineEndings.text(currentEditor.document.getLineEndings());
$statusLineEndings.attr("title", Strings.STATUSBAR_LINE_ENDINGS_TOOLTIP);
}

/**
* Handle active editor change event
* @param {Event} event (unused)
Expand Down Expand Up @@ -279,6 +307,7 @@ define(function (require, exports, module) {
_updateLanguageInfo(current);
_updateFileInfo(current);
_initOverwriteMode(current);
_initLineEndings(current);
_updateIndentType(fullPath);
_updateIndentSize(fullPath);
}
Expand Down Expand Up @@ -316,6 +345,7 @@ define(function (require, exports, module) {
$indentWidthLabel = $("#indent-width-label");
$indentWidthInput = $("#indent-width-input");
$statusOverwrite = $("#status-overwrite");
$statusLineEndings = $("#status-line-endings");

languageSelect = new DropdownButton("", [], function (item, index) {
var document = EditorManager.getActiveEditor().document,
Expand Down Expand Up @@ -389,6 +419,8 @@ define(function (require, exports, module) {
}
});

$statusLineEndings.on("click", _toggleLineEndings);

$statusOverwrite.on("click", _updateEditorOverwriteMode);
}

Expand Down
3 changes: 3 additions & 0 deletions src/nls/root/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,9 @@ define({
"STATUSBAR_CODE_INSPECTION_TOOLTIP" : "{0}. Click to toggle report panel.",
"STATUSBAR_DEFAULT_LANG" : "(default)",
"STATUSBAR_SET_DEFAULT_LANG" : "Set as Default for .{0} Files",
"STATUSBAR_LINE_ENDINGS_CRLF" : "CRLF",
"STATUSBAR_LINE_ENDINGS_LF" : "LF",
"STATUSBAR_LINE_ENDINGS_TOOLTIP" : "Click to toggle line endings",

// CodeInspection: errors/warnings
"ERRORS_PANEL_TITLE_MULTIPLE" : "{0} Problems",
Expand Down
17 changes: 16 additions & 1 deletion src/styles/brackets.less
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ a, img {
margin-right: 3px;
}

#status-overwrite:hover, #indent-type:hover, #indent-width-label:hover {
#status-overwrite:hover, #indent-type:hover, #indent-width-label:hover, #status-line-endings:hover {
text-decoration: underline;
}

Expand Down Expand Up @@ -323,6 +323,21 @@ a, img {
background-color: rgb(120, 178, 242);
}

#status-line-endings {
transition: background-color 3s;
background-color: rgba(255, 255, 255, 0);
color: @bc-text;
cursor: pointer;

.dark & {
color: @dark-bc-text;
}
}

#status-line-endings.flash {
transition: background-color 1s;
background-color: rgb(120, 178, 242);
}

#editor-holder {
position: relative;
Expand Down
1 change: 1 addition & 0 deletions src/widgets/StatusBar.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</div>
<div id="status-language"></div>
<div id="status-overwrite">{{STATUSBAR_INSERT}}</div>
<div id="status-line-endings">{{Strings.STATUSBAR_LINE_ENDINGS_CRLF}}</div>
<div class="spinner"></div>
</div>
</div>