Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
Merge pull request #165 from prettier/robwise/refactoring
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
robwise committed May 14, 2017
2 parents 7846ef1 + e1a9b39 commit 93f3b78
Show file tree
Hide file tree
Showing 128 changed files with 3,174 additions and 2,332 deletions.
10 changes: 6 additions & 4 deletions decls/index.js
Expand Up @@ -15,7 +15,6 @@ declare type TextEditor = {
getLastCursor: () => { getScopeDescriptor: () => string },
getSelectedText: () => string,
getSelectedBufferRanges: () => Ranges,
// getTextInRange: () => string,
getTextInBufferRange: () => string,
setCursorScreenPosition: (point: Point) => Point,
setTextInBufferRange: (bufferRange: Range, text: string) => Range,
Expand All @@ -36,6 +35,8 @@ declare type Atom$Disposable = any;
declare type Atom$View = any;
declare type Atom$Workspace = any;
declare type Atom$Command = { name: string, displayName: string };
declare type Atom$Notifications$Options = { detail?: ?string, dismissable?: ?boolean };
declare type Atom$Tooltips$Options = { title?: string };
declare var atom: {
commands: {
dispatch: (view: Atom$View, commandName: string) => void,
Expand All @@ -46,11 +47,12 @@ declare var atom: {
set: (key: string) => any,
},
notifications: {
addError: (message: string, options?: { detail?: string, dismissable?: boolean }) => void,
addInfo: (message: string, options?: { detail?: string, dismissable?: boolean }) => void,
addError: (message: string, options?: Atom$Notifications$Options) => void,
addInfo: (message: string, options?: Atom$Notifications$Options) => void,
addWarning: (message: string, options?: Atom$Notifications$Options) => void,
},
tooltips: {
add: (target: HTMLElement, options?: { title?: string }) => Atom$Disposable
add: (target: HTMLElement, options?: Atom$Tooltips$Options) => Atom$Disposable,
},
views: {
getView: Atom$Workspace => Atom$View,
Expand Down
123 changes: 123 additions & 0 deletions dist/atomInterface/index.js
@@ -0,0 +1,123 @@
'use strict';

// constants
var LINTER_LINT_COMMAND = 'linter:lint';

// local helpers
var getConfigOption = function getConfigOption(key) {
return atom.config.get('prettier-atom.' + key);
};

var setConfigOption = function setConfigOption(key, value) {
return atom.config.set('prettier-atom.' + key, value);
};

var isLinterLintCommandDefined = function isLinterLintCommandDefined(editor) {
return atom.commands.findCommands({ target: atom.views.getView(editor) }).some(function (command) {
return command.name === LINTER_LINT_COMMAND;
});
};

// public
var isLinterEslintAutofixEnabled = function isLinterEslintAutofixEnabled() {
return atom.config.get('linter-eslint.fixOnSave');
};

var shouldUseEslint = function shouldUseEslint() {
return getConfigOption('useEslint');
};

var shouldUseEditorConfig = function shouldUseEditorConfig() {
return getConfigOption('useEditorConfig');
};

var shouldDisplayErrors = function shouldDisplayErrors() {
return !getConfigOption('silenceErrors');
};

var isFormatOnSaveEnabled = function isFormatOnSaveEnabled() {
return getConfigOption('formatOnSaveOptions.enabled');
};

var shouldRespectEslintignore = function shouldRespectEslintignore() {
return getConfigOption('formatOnSaveOptions.respectEslintignore');
};

var getScopes = function getScopes() {
return getConfigOption('formatOnSaveOptions.scopes');
};

var getWhitelistedGlobs = function getWhitelistedGlobs() {
return getConfigOption('formatOnSaveOptions.whitelistedGlobs');
};

var getExcludedGlobs = function getExcludedGlobs() {
return getConfigOption('formatOnSaveOptions.excludedGlobs');
};

var toggleFormatOnSave = function toggleFormatOnSave() {
return setConfigOption('formatOnSaveOptions.enabled', !isFormatOnSaveEnabled());
};

var getAtomTabLength = function getAtomTabLength(editor) {
return atom.config.get('editor.tabLength', { scope: editor.getLastCursor().getScopeDescriptor() });
};

var getPrettierOptions = function getPrettierOptions() {
return getConfigOption('prettierOptions');
};

var getPrettierEslintOptions = function getPrettierEslintOptions() {
return getConfigOption('prettierEslintOptions');
};

var getAtomVersion = function getAtomVersion() {
return atom.getVersion();
};

var getPrettierAtomConfig = function getPrettierAtomConfig() {
return atom.config.get('prettier-atom');
};

var addTooltip = function addTooltip(element, options) {
return atom.tooltips.add(element, options);
};

var addInfoNotification = function addInfoNotification(message, options) {
return atom.notifications.addInfo(message, options);
};

var addWarningNotification = function addWarningNotification(message, options) {
return atom.notifications.addWarning(message, options);
};

var addErrorNotification = function addErrorNotification(message, options) {
return atom.notifications.addError(message, options);
};

var runLinter = function runLinter(editor) {
return isLinterLintCommandDefined(editor) && atom.commands.dispatch(atom.views.getView(editor), LINTER_LINT_COMMAND);
};

module.exports = {
addErrorNotification: addErrorNotification,
addInfoNotification: addInfoNotification,
addTooltip: addTooltip,
addWarningNotification: addWarningNotification,
getAtomTabLength: getAtomTabLength,
getAtomVersion: getAtomVersion,
getExcludedGlobs: getExcludedGlobs,
getPrettierAtomConfig: getPrettierAtomConfig,
getPrettierEslintOptions: getPrettierEslintOptions,
getPrettierOptions: getPrettierOptions,
getScopes: getScopes,
getWhitelistedGlobs: getWhitelistedGlobs,
isFormatOnSaveEnabled: isFormatOnSaveEnabled,
isLinterEslintAutofixEnabled: isLinterEslintAutofixEnabled,
runLinter: runLinter,
shouldDisplayErrors: shouldDisplayErrors,
shouldRespectEslintignore: shouldRespectEslintignore,
shouldUseEditorConfig: shouldUseEditorConfig,
shouldUseEslint: shouldUseEslint,
toggleFormatOnSave: toggleFormatOnSave
};
2 changes: 1 addition & 1 deletion dist/config-schema.json
Expand Up @@ -15,7 +15,7 @@
},
"useEditorConfig": {
"title": "EditorConfig Integration",
"description": "Use [EditorConfig](http://editorconfig.org/) configuration to load project-based settings for prettier formatting <br />Next options will be overriden: **Use tabs**, **Tab width**, **Print width**",
"description": "Use [EditorConfig](http://editorconfig.org/) configuration to load project-based settings for prettier formatting <br />Takes precedence over your settings. The following settings will be overriden if they are found in your .editorconfig: **Use tabs**, **Tab width**, **Print width**",
"type": "boolean",
"default": true,
"order": 3
Expand Down
15 changes: 0 additions & 15 deletions dist/displayDebugInfo.js

This file was deleted.

26 changes: 26 additions & 0 deletions dist/displayDebugInfo/index.js
@@ -0,0 +1,26 @@
'use strict';

var readPkg = require('read-pkg');
var path = require('path');

var _require = require('../atomInterface'),
getAtomVersion = _require.getAtomVersion,
getPrettierAtomConfig = _require.getPrettierAtomConfig,
addInfoNotification = _require.addInfoNotification;

var getDepPath = function getDepPath(dep) {
return path.join(__dirname, '..', '..', 'node_modules', dep);
};

var getDebugInfo = function getDebugInfo() {
return ('\nAtom version: ' + getAtomVersion() + '\nprettier-atom version: ' + readPkg.sync().version + '\nprettier version: ' + readPkg.sync(getDepPath('prettier')).version + '\nprettier-eslint version: ' + readPkg.sync(getDepPath('prettier-eslint')).version + '\nprettier-atom configuration: ' + JSON.stringify(getPrettierAtomConfig(), null, 2) + '\n').trim();
};

var displayDebugInfo = function displayDebugInfo() {
return addInfoNotification('prettier-atom: details on current install', {
detail: getDebugInfo(),
dismissable: true
});
};

module.exports = displayDebugInfo;
26 changes: 26 additions & 0 deletions dist/editorInterface/index.js
@@ -0,0 +1,26 @@
'use strict';

var EMBEDDED_SCOPES = ['text.html.vue', 'text.html.basic'];

var getBufferRange = function getBufferRange(editor) {
return editor.getBuffer().getRange();
};

var getCurrentScope = function getCurrentScope(editor) {
return editor.getGrammar().scopeName;
};

var isCurrentScopeEmbeddedScope = function isCurrentScopeEmbeddedScope(editor) {
return EMBEDDED_SCOPES.includes(getCurrentScope(editor));
};

var getCurrentFilePath = function getCurrentFilePath(editor) {
return editor.buffer.file ? editor.buffer.file.path : undefined;
};

module.exports = {
getBufferRange: getBufferRange,
isCurrentScopeEmbeddedScope: isCurrentScopeEmbeddedScope,
getCurrentScope: getCurrentScope,
getCurrentFilePath: getCurrentFilePath
};
94 changes: 0 additions & 94 deletions dist/executePrettier.js

This file was deleted.

41 changes: 41 additions & 0 deletions dist/executePrettier/buildEditorConfigOptions.js
@@ -0,0 +1,41 @@
'use strict';

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _ = require('lodash/fp');
var editorconfig = require('editorconfig');

var maxLineLengthIsNumber = _.flow(_.get('max_line_length'), _.isNumber);

var indentStyleIsTab = _.conforms({ indent_style: _.isEqual('tab') });

var indentSizeIsTab = _.conforms({ indent_size: _.isEqual('tab') });

var tabWidthIsNumber = _.conforms({ tab_width: _.isNumber });

var shouldUseTabWidth = _.overEvery([tabWidthIsNumber, _.overSome([indentSizeIsTab, indentStyleIsTab])]);

var indentSizeIsNumber = _.conforms({ indent_size: _.isNumber });

var indentStyleIsSpace = _.conforms({ indent_style: _.isEqual('space') });

var getPrintWidth = _.cond([[maxLineLengthIsNumber, function (opts) {
return { printWidth: opts.max_line_length };
}]]);

var getTabWidth = _.cond([[shouldUseTabWidth, function (opts) {
return { tabWidth: opts.tab_width };
}], [indentSizeIsNumber, function (opts) {
return { tabWidth: opts.indent_size };
}]]);

var getUseTabs = _.cond([[indentStyleIsTab, _.constant({ useTabs: true })], [indentStyleIsSpace, _.constant({ useTabs: false })]]);

var mapEditorConfigOptions = function mapEditorConfigOptions() {
var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return _extends({}, getPrintWidth(opts), getTabWidth(opts), getUseTabs(opts));
};

var buildEditorConfigOptions = _.flow(editorconfig.parseSync, mapEditorConfigOptions);

module.exports = buildEditorConfigOptions;

0 comments on commit 93f3b78

Please sign in to comment.