Reformat all JS files using prettier

This commit is contained in:
Rafael Oleza 2019-05-31 20:26:19 +02:00
parent 41840abdc2
commit 66f7f1746a
4 changed files with 193 additions and 159 deletions

View File

@ -16,23 +16,30 @@ module.exports = class GrammarListView {
element.textContent = grammarName;
element.dataset.grammar = grammarName;
const div = document.createElement('div')
div.classList.add('pull-right')
const div = document.createElement('div');
div.classList.add('pull-right');
if (isTreeSitter(grammar)) {
const parser = document.createElement('span')
parser.classList.add('grammar-selector-parser', 'badge', 'badge-success')
parser.textContent = 'Tree-sitter'
parser.setAttribute('title', '(Recommended) A faster parser with improved syntax highlighting & code navigation support.')
div.appendChild(parser)
const parser = document.createElement('span');
parser.classList.add(
'grammar-selector-parser',
'badge',
'badge-success'
);
parser.textContent = 'Tree-sitter';
parser.setAttribute(
'title',
'(Recommended) A faster parser with improved syntax highlighting & code navigation support.'
);
div.appendChild(parser);
}
if (grammar.scopeName) {
const scopeName = document.createElement('scopeName')
scopeName.classList.add('badge', 'badge-info')
scopeName.textContent = grammar.scopeName
div.appendChild(scopeName)
element.appendChild(div)
const scopeName = document.createElement('scopeName');
scopeName.classList.add('badge', 'badge-info');
scopeName.textContent = grammar.scopeName;
div.appendChild(scopeName);
element.appendChild(div);
}
return element;
@ -42,7 +49,7 @@ module.exports = class GrammarListView {
if (grammar === this.autoDetect) {
atom.textEditors.clearGrammarOverride(this.editor);
} else {
atom.grammars.assignGrammar(this.editor, grammar)
atom.grammars.assignGrammar(this.editor, grammar);
}
},
didCancelSelection: () => {
@ -80,65 +87,73 @@ module.exports = class GrammarListView {
async toggle() {
if (this.panel != null) {
this.cancel()
return
this.cancel();
return;
}
const editor = atom.workspace.getActiveTextEditor()
const editor = atom.workspace.getActiveTextEditor();
if (editor) {
this.editor = editor
this.currentGrammar = this.editor.getGrammar()
this.editor = editor;
this.currentGrammar = this.editor.getGrammar();
if (this.currentGrammar === atom.grammars.nullGrammar) {
this.currentGrammar = this.autoDetect;
}
let grammars = atom.grammars.getGrammars({includeTreeSitter: true}).filter(grammar => {
return grammar !== atom.grammars.nullGrammar && grammar.name
})
let grammars = atom.grammars
.getGrammars({ includeTreeSitter: true })
.filter(grammar => {
return grammar !== atom.grammars.nullGrammar && grammar.name;
});
if (atom.config.get('grammar-selector.hideDuplicateTextMateGrammars')) {
const oldGrammars = grammars
grammars = []
const blacklist = new Set()
const oldGrammars = grammars;
grammars = [];
const blacklist = new Set();
for (const grammar of oldGrammars) {
if (isTreeSitter(grammar)) {
blacklist.add(grammar.name)
grammars.push(grammar)
blacklist.add(grammar.name);
grammars.push(grammar);
}
}
atom.grammars.getGrammars({includeTreeSitter: false}).forEach(grammar => {
if (grammar !== atom.grammars.nullGrammar && grammar.name && !blacklist.has(grammar.name)) {
grammars.push(grammar)
}
})
atom.grammars
.getGrammars({ includeTreeSitter: false })
.forEach(grammar => {
if (
grammar !== atom.grammars.nullGrammar &&
grammar.name &&
!blacklist.has(grammar.name)
) {
grammars.push(grammar);
}
});
}
grammars.sort((a, b) => {
if (a.scopeName === 'text.plain') {
return -1;
} else if (b.scopeName === 'text.plain') {
return 1
return 1;
} else if (a.name === b.name) {
return compareGrammarType(a, b)
return compareGrammarType(a, b);
}
return a.name.localeCompare(b.name)
})
grammars.unshift(this.autoDetect)
await this.selectListView.update({ items: grammars })
this.attach()
return a.name.localeCompare(b.name);
});
grammars.unshift(this.autoDetect);
await this.selectListView.update({ items: grammars });
this.attach();
}
}
};
function isTreeSitter(grammar) {
return grammar.constructor.name === 'TreeSitterGrammar';
}
function isTreeSitter (grammar) {
return grammar.constructor.name === 'TreeSitterGrammar'
}
function compareGrammarType (a, b) {
function compareGrammarType(a, b) {
if (isTreeSitter(a)) {
return -1
return -1;
} else if (isTreeSitter(b)) {
return 1
return 1;
}
return 0
return 0;
}

View File

@ -5,9 +5,9 @@ describe('GrammarSelector', () => {
let [editor, textGrammar, jsGrammar] = [];
beforeEach(async () => {
jasmine.attachToDOM(atom.views.getView(atom.workspace))
atom.config.set('grammar-selector.showOnRightSideOfStatusBar', false)
atom.config.set('grammar-selector.hideDuplicateTextMateGrammars', false)
jasmine.attachToDOM(atom.views.getView(atom.workspace));
atom.config.set('grammar-selector.showOnRightSideOfStatusBar', false);
atom.config.set('grammar-selector.hideDuplicateTextMateGrammars', false);
await atom.packages.activatePackage('status-bar');
await atom.packages.activatePackage('grammar-selector');
@ -28,55 +28,61 @@ describe('GrammarSelector', () => {
describe('when grammar-selector:show is triggered', () =>
it('displays a list of all the available grammars', async () => {
const grammarView = (await getGrammarView(editor)).element
const grammarView = (await getGrammarView(editor)).element;
// -1 for removing nullGrammar, +1 for adding "Auto Detect"
// Tree-sitter names the regex and JSDoc grammars
expect(grammarView.querySelectorAll('li').length).toBe(atom.grammars.grammars.filter(g => g.name).length)
expect(grammarView.querySelectorAll('li')[0].textContent).toBe('Auto Detect')
expect(grammarView.textContent.includes('source.a')).toBe(false)
grammarView.querySelectorAll('li').forEach(
li => expect(li.textContent).not.toBe(atom.grammars.nullGrammar.name)
)
expect(grammarView.textContent.includes('Tree-sitter')).toBe(true) // check we are showing and labelling Tree-sitter grammars
})
)
expect(grammarView.querySelectorAll('li').length).toBe(
atom.grammars.grammars.filter(g => g.name).length
);
expect(grammarView.querySelectorAll('li')[0].textContent).toBe(
'Auto Detect'
);
expect(grammarView.textContent.includes('source.a')).toBe(false);
grammarView
.querySelectorAll('li')
.forEach(li =>
expect(li.textContent).not.toBe(atom.grammars.nullGrammar.name)
);
expect(grammarView.textContent.includes('Tree-sitter')).toBe(true); // check we are showing and labelling Tree-sitter grammars
}));
describe('when a grammar is selected', () =>
it('sets the new grammar on the editor', async () => {
const grammarView = await getGrammarView(editor)
grammarView.props.didConfirmSelection(textGrammar)
expect(editor.getGrammar()).toBe(textGrammar)
}))
const grammarView = await getGrammarView(editor);
grammarView.props.didConfirmSelection(textGrammar);
expect(editor.getGrammar()).toBe(textGrammar);
}));
describe('when auto-detect is selected', () =>
it('restores the auto-detected grammar on the editor', async () => {
let grammarView = await getGrammarView(editor)
grammarView.props.didConfirmSelection(textGrammar)
expect(editor.getGrammar()).toBe(textGrammar)
let grammarView = await getGrammarView(editor);
grammarView.props.didConfirmSelection(textGrammar);
expect(editor.getGrammar()).toBe(textGrammar);
grammarView = await getGrammarView(editor)
grammarView.props.didConfirmSelection(grammarView.items[0])
expect(editor.getGrammar()).toBe(jsGrammar)
}))
grammarView = await getGrammarView(editor);
grammarView.props.didConfirmSelection(grammarView.items[0]);
expect(editor.getGrammar()).toBe(jsGrammar);
}));
describe("when the editor's current grammar is the null grammar", () =>
it('displays Auto Detect as the selected grammar', async () => {
editor.setGrammar(atom.grammars.nullGrammar)
const grammarView = (await getGrammarView(editor)).element
expect(grammarView.querySelector('li.active').textContent).toBe('Auto Detect')
})
)
editor.setGrammar(atom.grammars.nullGrammar);
const grammarView = (await getGrammarView(editor)).element;
expect(grammarView.querySelector('li.active').textContent).toBe(
'Auto Detect'
);
}));
describe('when editor is untitled', () =>
it('sets the new grammar on the editor', async () => {
editor = await atom.workspace.open();
expect(editor.getGrammar()).not.toBe(jsGrammar);
const grammarView = await getGrammarView(editor)
grammarView.props.didConfirmSelection(jsGrammar)
expect(editor.getGrammar()).toBe(jsGrammar)
}))
const grammarView = await getGrammarView(editor);
grammarView.props.didConfirmSelection(jsGrammar);
expect(editor.getGrammar()).toBe(jsGrammar);
}));
describe('Status bar grammar label', () => {
let [grammarStatus, grammarTile, statusBar] = [];
@ -175,62 +181,69 @@ describe('GrammarSelector', () => {
describe('when toggling hideDuplicateTextMateGrammars', () => {
it('shows only the Tree-sitter if true and both exist', async () => {
// the main JS grammar has both a TextMate and Tree-sitter implementation
atom.config.set('grammar-selector.hideDuplicateTextMateGrammars', true)
const grammarView = await getGrammarView(editor)
const observedNames = new Set()
atom.config.set('grammar-selector.hideDuplicateTextMateGrammars', true);
const grammarView = await getGrammarView(editor);
const observedNames = new Set();
grammarView.element.querySelectorAll('li').forEach(li => {
const name = li.getAttribute('data-grammar')
expect(observedNames.has(name)).toBe(false)
observedNames.add(name)
})
const name = li.getAttribute('data-grammar');
expect(observedNames.has(name)).toBe(false);
observedNames.add(name);
});
// check the seen JS is actually the Tree-sitter one
const list = atom.workspace.getModalPanels()[0].item
const list = atom.workspace.getModalPanels()[0].item;
for (const item of list.items) {
if (item.name === 'JavaScript') {
expect(item.constructor.name === 'TreeSitterGrammar')
expect(item.constructor.name === 'TreeSitterGrammar');
}
}
})
});
it('shows both if false', async () => {
await atom.packages.activatePackage('language-c') // punctuation making it sort wrong
atom.config.set('grammar-selector.hideDuplicateTextMateGrammars', false)
await getGrammarView(editor)
let cppCount = 0
await atom.packages.activatePackage('language-c'); // punctuation making it sort wrong
atom.config.set(
'grammar-selector.hideDuplicateTextMateGrammars',
false
);
await getGrammarView(editor);
let cppCount = 0;
const listItems = atom.workspace.getModalPanels()[0].item.items
const listItems = atom.workspace.getModalPanels()[0].item.items;
for (let i = 0; i < listItems.length; i++) {
const grammar = listItems[i]
const name = grammar.name
const grammar = listItems[i];
const name = grammar.name;
if (cppCount === 0 && name === 'C++') {
expect(grammar.constructor.name).toBe('TreeSitterGrammar') // first C++ entry should be Tree-sitter
cppCount++
expect(grammar.constructor.name).toBe('TreeSitterGrammar'); // first C++ entry should be Tree-sitter
cppCount++;
} else if (cppCount === 1) {
expect(name).toBe('C++')
expect(grammar.constructor.name).toBe('Grammar') // immediate next grammar should be the TextMate version
cppCount++
expect(name).toBe('C++');
expect(grammar.constructor.name).toBe('Grammar'); // immediate next grammar should be the TextMate version
cppCount++;
} else {
expect(name).not.toBe('C++') // there should not be any other C++ grammars
expect(name).not.toBe('C++'); // there should not be any other C++ grammars
}
}
expect(cppCount).toBe(2) // ensure we actually saw both grammars
})
})
expect(cppCount).toBe(2); // ensure we actually saw both grammars
});
});
describe('for every Tree-sitter grammar', () => {
it('adds a label to identify it as Tree-sitter', async () => {
const grammarView = await getGrammarView(editor)
const elements = grammarView.element.querySelectorAll('li')
const listItems = atom.workspace.getModalPanels()[0].item.items
const grammarView = await getGrammarView(editor);
const elements = grammarView.element.querySelectorAll('li');
const listItems = atom.workspace.getModalPanels()[0].item.items;
for (let i = 0; i < listItems.length; i++) {
if (listItems[i].constructor.name === 'TreeSitterGrammar') {
expect(elements[i].childNodes[1].childNodes[0].className.startsWith('grammar-selector-parser')).toBe(true)
expect(
elements[i].childNodes[1].childNodes[0].className.startsWith(
'grammar-selector-parser'
)
).toBe(true);
}
}
})
})
});
});
describe('when clicked', () =>
it('shows the grammar selector modal', () => {
@ -258,8 +271,8 @@ function getTooltipText(element) {
return tooltip.getTitle();
}
async function getGrammarView (editor) {
atom.commands.dispatch(editor.getElement(), 'grammar-selector:show')
await SelectListView.getScheduler().getNextUpdatePromise()
return atom.workspace.getModalPanels()[0].getItem()
async function getGrammarView(editor) {
atom.commands.dispatch(editor.getElement(), 'grammar-selector:show');
await SelectListView.getScheduler().getNextUpdatePromise();
return atom.workspace.getModalPanels()[0].getItem();
}

View File

@ -72,17 +72,19 @@ describe('GrammarRegistry', () => {
describe('.assignGrammar(buffer, grammar)', () => {
it('allows a TextMate grammar to be assigned directly, even when Tree-sitter is permitted', () => {
grammarRegistry.loadGrammarSync(
require.resolve('language-javascript/grammars/tree-sitter-javascript.cson')
)
require.resolve(
'language-javascript/grammars/tree-sitter-javascript.cson'
)
);
const tmGrammar = grammarRegistry.loadGrammarSync(
require.resolve('language-javascript/grammars/javascript.cson')
)
);
const buffer = new TextBuffer()
expect(grammarRegistry.assignGrammar(buffer, tmGrammar)).toBe(true)
expect(buffer.getLanguageMode().getGrammar()).toBe(tmGrammar)
})
})
const buffer = new TextBuffer();
expect(grammarRegistry.assignGrammar(buffer, tmGrammar)).toBe(true);
expect(buffer.getLanguageMode().getGrammar()).toBe(tmGrammar);
});
});
describe('.grammarForId(languageId)', () => {
it('returns a text-mate grammar when `core.useTreeSitterParsers` is false', () => {
@ -869,34 +871,34 @@ describe('GrammarRegistry', () => {
grammarRegistryCopy.loadGrammarSync(
require.resolve('language-javascript/grammars/javascript.cson')
)
expect(buffer1Copy.getLanguageMode().getLanguageId()).toBe('source.c')
expect(buffer2Copy.getLanguageMode().getLanguageId()).toBe('source.js')
})
})
);
expect(buffer1Copy.getLanguageMode().getLanguageId()).toBe('source.c');
expect(buffer2Copy.getLanguageMode().getLanguageId()).toBe('source.js');
});
});
describe('when working with grammars', () => {
beforeEach(async () => {
await atom.packages.activatePackage('language-javascript')
})
await atom.packages.activatePackage('language-javascript');
});
it('returns both Tree-sitter and TextMate grammars by default', async () => {
const allGrammars = atom.grammars.getGrammars()
const tmGrammars = atom.grammars.getGrammars({textMateOnly: true})
expect(allGrammars.length).toBeGreaterThan(tmGrammars.length)
})
const allGrammars = atom.grammars.getGrammars();
const tmGrammars = atom.grammars.getGrammars({ textMateOnly: true });
expect(allGrammars.length).toBeGreaterThan(tmGrammars.length);
});
it('executes the foreach callback on both Tree-sitter and TextMate grammars', async () => {
const numAllGrammars = atom.grammars.getGrammars().length
let i = 0
atom.grammars.forEachGrammar(() => i++)
expect(i).toBe(numAllGrammars)
})
})
})
const numAllGrammars = atom.grammars.getGrammars().length;
let i = 0;
atom.grammars.forEachGrammar(() => i++);
expect(i).toBe(numAllGrammars);
});
});
});
function retainedBufferCount (grammarRegistry) {
return grammarRegistry.grammarScoresByBuffer.size
function retainedBufferCount(grammarRegistry) {
return grammarRegistry.grammarScoresByBuffer.size;
}
function subscriptionCount(grammarRegistry) {

View File

@ -155,15 +155,17 @@ module.exports = class GrammarRegistry {
// * `grammar` The desired {Grammar}.
//
// Returns a {Boolean} that indicates whether the assignment was sucessful
assignGrammar (buffer, grammar) {
if (!grammar) return false
if (buffer.getBuffer) buffer = buffer.getBuffer()
this.languageOverridesByBufferId.set(buffer.id, grammar.scopeName || null)
this.grammarScoresByBuffer.set(buffer, null)
assignGrammar(buffer, grammar) {
if (!grammar) return false;
if (buffer.getBuffer) buffer = buffer.getBuffer();
this.languageOverridesByBufferId.set(buffer.id, grammar.scopeName || null);
this.grammarScoresByBuffer.set(buffer, null);
if (grammar !== buffer.getLanguageMode().grammar) {
buffer.setLanguageMode(this.languageModeForGrammarAndBuffer(grammar, buffer))
buffer.setLanguageMode(
this.languageModeForGrammarAndBuffer(grammar, buffer)
);
}
return true
return true;
}
// Extended: Get the `languageId` that has been explicitly assigned to
@ -347,8 +349,8 @@ module.exports = class GrammarRegistry {
}
}
forEachGrammar (callback) {
this.grammars.forEach(callback)
forEachGrammar(callback) {
this.grammars.forEach(callback);
}
grammarForId(languageId) {
@ -495,8 +497,8 @@ module.exports = class GrammarRegistry {
return this.textmateRegistry.nullGrammar;
}
get grammars () {
return this.getGrammars()
get grammars() {
return this.getGrammars();
}
decodeTokens() {
@ -620,12 +622,14 @@ module.exports = class GrammarRegistry {
// [Tree-sitter](https://github.blog/2018-10-31-atoms-new-parsing-system/) grammars
//
// Returns a non-empty {Array} of {Grammar} instances.
getGrammars (params) {
let tmGrammars = this.textmateRegistry.getGrammars()
if (params && params.textMateOnly) return tmGrammars
getGrammars(params) {
let tmGrammars = this.textmateRegistry.getGrammars();
if (params && params.textMateOnly) return tmGrammars;
const tsGrammars = Object.values(this.treeSitterGrammarsById).filter(g => g.scopeName)
return tmGrammars.concat(tsGrammars) // NullGrammar is expected to be first
const tsGrammars = Object.values(this.treeSitterGrammarsById).filter(
g => g.scopeName
);
return tmGrammars.concat(tsGrammars); // NullGrammar is expected to be first
}
scopeForId(id) {