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
43 changes: 40 additions & 3 deletions src/editor/EditorStatusBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,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 @@ -216,7 +217,7 @@ define(function (require, exports, module) {
if (!doNotAnimate) {
AnimationUtils.animateUsingClass($statusOverwrite[0], "flash", 1500);
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: trailing whitespace


/**
* Update insert/overwrite indicator
Expand All @@ -231,6 +232,29 @@ define(function (require, exports, module) {
editor.toggleOverwrite(newstate);
}

/**
* Update LineEndings indicator
* @param {Event} event (unused)
*/
function _updateLineEndings(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.

currentLineEndings;

// update label
if($statusLineEndings.text() === Strings.STATUSBAR_LINE_ENDINGS_CRLF) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: space between if and (

$statusLineEndings.text(Strings.STATUSBAR_LINE_ENDINGS_LF);
currentLineEndings = FileUtils.LINE_ENDINGS_LF;
} else {
$statusLineEndings.text(Strings.STATUSBAR_LINE_ENDINGS_CRLF);
currentLineEndings = FileUtils.LINE_ENDINGS_CRLF;
}
// Update the line ending in the document if needed.
if(document.getLineEndings() !== currentLineEndings) {
document.setLineEndings(currentLineEndings);
}
}

/**
* Initialize insert/overwrite indicator
* @param {Editor} currentEditor Current editor
Expand All @@ -240,6 +264,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 +312,7 @@ define(function (require, exports, module) {
_updateLanguageInfo(current);
_updateFileInfo(current);
_initOverwriteMode(current);
_initLineEndings(current);
_updateIndentType(fullPath);
_updateIndentSize(fullPath);
}
Expand Down Expand Up @@ -316,7 +350,8 @@ 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,
defaultLang = LanguageManager.getLanguageForPath(document.file.fullPath, true);
Expand Down Expand Up @@ -389,6 +424,8 @@ define(function (require, exports, module) {
}
});

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

$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>