mirror of
https://github.com/pulsar-edit/pulsar.git
synced 2024-09-11 11:15:24 +03:00
Allow hiding duplicates + stable sort TS
This commit is contained in:
parent
5ff44a1298
commit
8d4b9c2485
@ -20,7 +20,7 @@ class GrammarListView {
|
||||
const div = document.createElement('div')
|
||||
div.classList.add('pull-right')
|
||||
|
||||
if (grammar.constructor.name === "TreeSitterGrammar") {
|
||||
if (isTreeSitter(grammar)) {
|
||||
const parser = document.createElement('span')
|
||||
parser.classList.add('grammar-selector-parser', 'badge', 'badge-success')
|
||||
parser.textContent = 'Tree-sitter'
|
||||
@ -82,28 +82,47 @@ class GrammarListView {
|
||||
async toggle () {
|
||||
if (this.panel != null) {
|
||||
this.cancel()
|
||||
} else if (atom.workspace.getActiveTextEditor()) {
|
||||
this.editor = atom.workspace.getActiveTextEditor()
|
||||
return
|
||||
}
|
||||
|
||||
const editor = atom.workspace.getActiveTextEditor()
|
||||
if (editor) {
|
||||
this.editor = editor
|
||||
this.currentGrammar = this.editor.getGrammar()
|
||||
if (this.currentGrammar === atom.grammars.nullGrammar) {
|
||||
this.currentGrammar = this.autoDetect
|
||||
}
|
||||
|
||||
const grammars = atom.grammars.getGrammars(true).filter((grammar) => {
|
||||
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()
|
||||
for (const grammar of oldGrammars) {
|
||||
if (isTreeSitter(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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
grammars.sort((a, b) => {
|
||||
if (a.scopeName === 'text.plain') {
|
||||
return -1
|
||||
} else if (b.scopeName === 'text.plain') {
|
||||
return 1
|
||||
} else if (a.name) {
|
||||
return a.name.localeCompare(b.name)
|
||||
} else if (a.scopeName) {
|
||||
return a.scopeName.localeCompare(b.scopeName)
|
||||
} else {
|
||||
return 1
|
||||
} else if (a.name === b.name) {
|
||||
return compareGrammarType(a, b)
|
||||
}
|
||||
return a.name.localeCompare(b.name)
|
||||
})
|
||||
grammars.unshift(this.autoDetect)
|
||||
await this.selectListView.update({items: grammars})
|
||||
@ -111,3 +130,16 @@ class GrammarListView {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function isTreeSitter (grammar) {
|
||||
return grammar.constructor.name === "TreeSitterGrammar"
|
||||
}
|
||||
|
||||
function compareGrammarType (a, b) {
|
||||
if (isTreeSitter(a)) {
|
||||
return -1
|
||||
} else if (isTreeSitter(b)) {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
@ -37,6 +37,11 @@
|
||||
"type": "boolean",
|
||||
"default": true,
|
||||
"description": "Show the active pane item's language on the right side of Atom's status bar, instead of the left."
|
||||
},
|
||||
"hideDuplicateTextMateGrammars": {
|
||||
"type": "boolean",
|
||||
"default": true,
|
||||
"description": "Hides the TextMate grammar when there is an existing Tree-sitter grammar"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -549,10 +549,12 @@ class GrammarRegistry {
|
||||
// Extended: Get all the grammars in this registry.
|
||||
//
|
||||
// Returns a non-empty {Array} of {Grammar} instances.
|
||||
getGrammars (includeTreesitter = false) {
|
||||
let grammars = this.textmateRegistry.getGrammars()
|
||||
if (includeTreesitter) grammars = grammars.concat(Object.values(this.treeSitterGrammarsById))
|
||||
return grammars
|
||||
getGrammars (params = {includeTreeSitter: false}) {
|
||||
let tmGrammars = this.textmateRegistry.getGrammars()
|
||||
if (!params.includeTreeSitter) return tmGrammars
|
||||
|
||||
let tsGrammars = Object.values(this.treeSitterGrammarsById)
|
||||
return tsGrammars.concat(tmGrammars)
|
||||
}
|
||||
|
||||
scopeForId (id) {
|
||||
|
Loading…
Reference in New Issue
Block a user