Skip to content

Commit

Permalink
Merge pull request #855 from savetheclocktower/modern-tree-sitter-on-…
Browse files Browse the repository at this point in the history
…by-default

Enable modern Tree-sitter grammars by default
  • Loading branch information
savetheclocktower authored Jan 11, 2024
2 parents bf3cfde + f283a34 commit 9cf8691
Show file tree
Hide file tree
Showing 57 changed files with 858 additions and 345 deletions.
2 changes: 1 addition & 1 deletion integration/workspace.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const languages = [
// {language: "gfm", code: '10', checks: {numeric: '10'}},
// {language: "git", code: '10', checks: {numeric: '10'}},
{language: "Go", code: '10', checks: {numeric: '10'}},
{language: "HTML", code: '<foo type="10"></foo>', checks: {string: '10'}},
{language: "HTML", code: '<foo type="10"></foo>', checks: {quoted: '"10"'}},
{language: "XML", code: '<foo type="10"></foo>', checks: {string: '"10"'}},
// {language: "hyperlink", code: '10', checks: {numeric: '10'}},
{language: "JSON", code: '10', checks: {numeric: '10'}},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@
"service-hub": "^0.7.4",
"settings-view": "file:packages/settings-view",
"sinon": "9.2.1",
"snippets": "github:pulsar-edit/snippets#ba70705",
"snippets": "github:pulsar-edit/snippets#6b9163415270b8757b262f5dfaafaa05d47324a9",
"solarized-dark-syntax": "file:packages/solarized-dark-syntax",
"solarized-light-syntax": "file:packages/solarized-light-syntax",
"spell-check": "file:packages/spell-check",
Expand Down
6 changes: 2 additions & 4 deletions packages/autocomplete-css/spec/provider-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const packagesToTest = {

const wait = (ms) => new Promise(resolve => setTimeout(resolve, ms));

const whenEditorReady = function(editor) {
const whenEditorReady = function (editor) {
const languageMode = editor.getBuffer().getLanguageMode();
if (!languageMode.constructor.name.includes('TreeSitter')) {
return Promise.resolve();
Expand Down Expand Up @@ -105,8 +105,6 @@ describe("CSS property name and value autocompletions", async () => {
await atom.workspace.open(packagesToTest[packageLabel].file);
editor = atom.workspace.getActiveTextEditor();
await whenEditorReady(editor);
console.warn('USING TREE SITTER?!?', packageLabel, meta.useTreeSitter);
atom.config.set('core.useExperimentalModernTreeSitter', meta.useTreeSitter ?? false);
atom.config.set('core.useTreeSitterParsers', meta.useTreeSitter ?? false);
});

Expand Down Expand Up @@ -738,7 +736,7 @@ div:nth {
})
);

Object.keys(packagesToTest).forEach(function(packageLabel) {
Object.keys(packagesToTest).forEach(function (packageLabel) {
if (packagesToTest[packageLabel].name !== 'language-css') {
describe(`${packageLabel} files`, async () => {
beforeEach(async () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/autocomplete-html/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const provider = {

getSuggestions (request) {
try {
if (request.editor.getBuffer().getLanguageMode().tree) {
let languageMode = request.editor.getBuffer().getLanguageMode();
if (languageMode.constructor.name === 'TreeSitterLanguageMode') {
return getSuggestionsWithTreeSitter(request)
} else {
return getSuggestionsWithTextMate(request)
Expand Down
26 changes: 25 additions & 1 deletion packages/autocomplete-html/lib/text-mate-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function isTagStart ({prefix, scopeDescriptor, bufferPosition, editor}) {
function isAttributeStart ({prefix, scopeDescriptor, bufferPosition, editor}) {
const scopes = scopeDescriptor.getScopesArray()
if (!getPreviousAttribute(editor, bufferPosition) && prefix && !prefix.trim()) {
return hasTagScope(scopes)
return hasTagScope(scopes) || afterTagScope(editor, bufferPosition)
}

const previousBufferPosition = [bufferPosition.row, Math.max(0, bufferPosition.column - 1)]
Expand All @@ -68,13 +68,37 @@ function isAttributeStart ({prefix, scopeDescriptor, bufferPosition, editor}) {
)
}

// This fixes the
//
// <div |
//
// scenario in Tree-sitter grammars — no closing `>` on the tag, so we should
// move back to the nearest text and try to read the scopes from there.
// Designed to work no matter how many spaces there are between the end of the
// tag name and the cursor.
function afterTagScope (editor, bufferPosition) {
let cursor = editor.getCursors().find(cursor => {
return cursor.getBufferPosition().isEqual(bufferPosition)
})
if (!cursor) return false;
let position = cursor.getPreviousWordBoundaryBufferPosition();
position = position.translate([0, -1]);
let scopes = editor.scopeDescriptorForBufferPosition(position);
return scopes.getScopesArray().some(t => t.startsWith('entity.name.tag'));
}

function isAttributeValueStart ({scopeDescriptor, bufferPosition, editor}) {
const scopes = scopeDescriptor.getScopesArray()

const previousBufferPosition = [bufferPosition.row, Math.max(0, bufferPosition.column - 1)]
const previousScopes = editor.scopeDescriptorForBufferPosition(previousBufferPosition)
const previousScopesArray = previousScopes.getScopesArray()

// This is an unambiguous case — if the cursor is on the right side of the
// opening quote, then we must be in the right place.
if (previousScopesArray.includes('punctuation.definition.string.begin.html'))
return true

// autocomplete here: attribute="|"
// not here: attribute=|""
// or here: attribute=""|
Expand Down
13 changes: 13 additions & 0 deletions packages/autocomplete-html/spec/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
env: { jasmine: true },
globals: {
"waitsForPromise": true,
"advanceClock": true
},
rules: {
"node/no-unpublished-require": "off",
"node/no-extraneous-require": "off",
"no-unused-vars": "off",
"no-empty": "off"
}
};
Loading

0 comments on commit 9cf8691

Please sign in to comment.