Skip to content

Commit

Permalink
Merge pull request #677 from savetheclocktower/tree-sitter-more-fixes
Browse files Browse the repository at this point in the history
Tree-sitter running fixes (August edition)
  • Loading branch information
savetheclocktower authored Sep 13, 2023
2 parents 4e55d8c + e9903ad commit 69038a1
Show file tree
Hide file tree
Showing 35 changed files with 612 additions and 168 deletions.
56 changes: 28 additions & 28 deletions packages/bracket-matcher/lib/bracket-matcher-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const MAX_ROWS_TO_SCAN_BACKWARD_TRAVERSAL = Object.freeze(Point(-MAX_ROWS_TO_SCA

module.exports =
class BracketMatcherView {
constructor (editor, editorElement, matchManager) {
constructor(editor, editorElement, matchManager) {
this.destroy = this.destroy.bind(this)
this.updateMatch = this.updateMatch.bind(this)
this.editor = editor
Expand Down Expand Up @@ -65,11 +65,11 @@ class BracketMatcherView {
this.updateMatch()
}

destroy () {
destroy() {
this.subscriptions.dispose()
}

updateMatch () {
updateMatch() {
if (this.pairHighlighted) {
this.editor.destroyMarker(this.startMarker.id)
this.editor.destroyMarker(this.endMarker.id)
Expand Down Expand Up @@ -113,13 +113,13 @@ class BracketMatcherView {
this.tagHighlighted = highlightTag
}

selectMatchingBrackets () {
selectMatchingBrackets() {
if (!this.bracket1Range && !this.bracket2Range) return
this.editor.setSelectedBufferRanges([this.bracket1Range, this.bracket2Range])
this.matchManager.changeBracketsMode = true
}

removeMatchingBrackets () {
removeMatchingBrackets() {
if (this.editor.hasMultipleCursors()) {
this.editor.backspace()
return
Expand Down Expand Up @@ -159,7 +159,7 @@ class BracketMatcherView {
})
}

findMatchingEndBracket (startBracketPosition, startBracket, endBracket) {
findMatchingEndBracket(startBracketPosition, startBracket, endBracket) {
if (startBracket === endBracket) return

if (this.hasSyntaxTree()) {
Expand All @@ -171,7 +171,7 @@ class BracketMatcherView {
}
}

findMatchingStartBracket (endBracketPosition, startBracket, endBracket) {
findMatchingStartBracket(endBracketPosition, startBracket, endBracket) {
if (startBracket === endBracket) return

if (this.hasSyntaxTree()) {
Expand All @@ -183,7 +183,7 @@ class BracketMatcherView {
}
}

findMatchingEndBracketWithSyntaxTree (bracketPosition, startBracket, endBracket) {
findMatchingEndBracketWithSyntaxTree(bracketPosition, startBracket, endBracket) {
let result
const bracketEndPosition = bracketPosition.traverse([0, startBracket.length])
this.editor.buffer.getLanguageMode().getSyntaxNodeContainingRange(
Expand All @@ -202,7 +202,7 @@ class BracketMatcherView {
return result
}

findMatchingStartBracketWithSyntaxTree (bracketPosition, startBracket, endBracket) {
findMatchingStartBracketWithSyntaxTree(bracketPosition, startBracket, endBracket) {
let result
const bracketEndPosition = bracketPosition.traverse([0, startBracket.length])
this.editor.buffer.getLanguageMode().getSyntaxNodeContainingRange(
Expand All @@ -221,7 +221,7 @@ class BracketMatcherView {
return result
}

findMatchingTagNameRangesWithSyntaxTree () {
findMatchingTagNameRangesWithSyntaxTree() {
const position = this.editor.getCursorBufferPosition()
const {startTag, endTag} = this.findContainingTagsWithSyntaxTree(position)
if (startTag && (startTag.range.containsPoint(position) || endTag.range.containsPoint(position))) {
Expand All @@ -244,7 +244,7 @@ class BracketMatcherView {
}
}

findMatchingTagsWithSyntaxTree () {
findMatchingTagsWithSyntaxTree() {
const position = this.editor.getCursorBufferPosition()
const {startTag, endTag} = this.findContainingTagsWithSyntaxTree(position)
if (startTag) {
Expand All @@ -254,7 +254,7 @@ class BracketMatcherView {
}
}

findContainingTagsWithSyntaxTree (position) {
findContainingTagsWithSyntaxTree(position) {
let startTag, endTag
if (position.column === this.editor.buffer.lineLengthForRow(position.row)) position.column--;
this.editor.buffer.getLanguageMode().getSyntaxNodeAtPosition(position, node => {
Expand All @@ -275,14 +275,14 @@ class BracketMatcherView {
startTag = firstChild
endTag = lastChild
}
return true
}
return true
}
})
return {startTag, endTag}
}

findMatchingEndBracketWithRegexSearch (startBracketPosition, startBracket, endBracket) {
findMatchingEndBracketWithRegexSearch(startBracketPosition, startBracket, endBracket) {
const scanRange = new Range(
startBracketPosition.traverse(ONE_CHAR_FORWARD_TRAVERSAL),
startBracketPosition.traverse(MAX_ROWS_TO_SCAN_FORWARD_TRAVERSAL)
Expand All @@ -308,7 +308,7 @@ class BracketMatcherView {
return endBracketPosition
}

findMatchingStartBracketWithRegexSearch (endBracketPosition, startBracket, endBracket) {
findMatchingStartBracketWithRegexSearch(endBracketPosition, startBracket, endBracket) {
const scanRange = new Range(
endBracketPosition.traverse(MAX_ROWS_TO_SCAN_BACKWARD_TRAVERSAL),
endBracketPosition
Expand All @@ -334,15 +334,15 @@ class BracketMatcherView {
return startBracketPosition
}

findPrecedingStartBracket (cursorPosition) {
findPrecedingStartBracket(cursorPosition) {
if (this.hasSyntaxTree()) {
return this.findPrecedingStartBracketWithSyntaxTree(cursorPosition)
} else {
return this.findPrecedingStartBracketWithRegexSearch(cursorPosition)
}
}

findPrecedingStartBracketWithSyntaxTree (cursorPosition) {
findPrecedingStartBracketWithSyntaxTree(cursorPosition) {
let result
this.editor.buffer.getLanguageMode().getSyntaxNodeAtPosition(cursorPosition, node => {
for (const child of node.children) {
Expand All @@ -359,7 +359,7 @@ class BracketMatcherView {
return result
}

findPrecedingStartBracketWithRegexSearch (cursorPosition) {
findPrecedingStartBracketWithRegexSearch(cursorPosition) {
const scanRange = new Range(Point.ZERO, cursorPosition)
const startBracket = _.escapeRegExp(_.keys(this.matchManager.pairedCharacters).join(''))
const endBracket = _.escapeRegExp(_.keys(this.matchManager.pairedCharactersInverse).join(''))
Expand All @@ -384,7 +384,7 @@ class BracketMatcherView {
return startPosition
}

createMarker (bufferRange) {
createMarker(bufferRange) {
const marker = this.editor.markBufferRange(bufferRange)
this.editor.decorateMarker(marker, {type: 'highlight', class: 'bracket-matcher', deprecatedRegionClass: 'bracket-matcher'})
if (atom.config.get('bracket-matcher.highlightMatchingLineNumber', {scope: this.editor.getRootScopeDescriptor()}) && this.gutter) {
Expand All @@ -393,7 +393,7 @@ class BracketMatcherView {
return marker
}

findCurrentPair () {
findCurrentPair() {
const currentPosition = this.editor.getCursorBufferPosition()
const previousPosition = currentPosition.traverse(ONE_CHAR_BACKWARD_TRAVERSAL)
const nextPosition = currentPosition.traverse(ONE_CHAR_FORWARD_TRAVERSAL)
Expand Down Expand Up @@ -422,7 +422,7 @@ class BracketMatcherView {
return {position, matchPosition, bracket: currentBracket}
}

goToMatchingBracket () {
goToMatchingBracket() {
if (!this.pairHighlighted) return this.gotoPrecedingStartBracket()
const position = this.editor.getCursorBufferPosition()

Expand Down Expand Up @@ -468,7 +468,7 @@ class BracketMatcherView {
}
}

gotoPrecedingStartBracket () {
gotoPrecedingStartBracket() {
if (this.pairHighlighted) return

const matchPosition = this.findPrecedingStartBracket(this.editor.getCursorBufferPosition())
Expand Down Expand Up @@ -522,7 +522,7 @@ class BracketMatcherView {
})
}

selectInsideBrackets () {
selectInsideBrackets() {
let endPosition, endRange, startPosition, startRange
if (this.pairHighlighted) {
startRange = this.startMarker.getBufferRange()
Expand Down Expand Up @@ -550,7 +550,7 @@ class BracketMatcherView {

// Insert at the current cursor position a closing tag if there exists an
// open tag that is not closed afterwards.
closeTag () {
closeTag() {
const cursorPosition = this.editor.getCursorBufferPosition()
const preFragment = this.editor.getTextInBufferRange([Point.ZERO, cursorPosition])
const postFragment = this.editor.getTextInBufferRange([cursorPosition, Point.INFINITY])
Expand All @@ -561,15 +561,15 @@ class BracketMatcherView {
}
}

isCursorOnCommentOrString () {
isCursorOnCommentOrString() {
return this.isScopeCommentedOrString(this.editor.getLastCursor().getScopeDescriptor().getScopesArray())
}

isRangeCommentedOrString (range) {
isRangeCommentedOrString(range) {
return this.isScopeCommentedOrString(this.editor.scopeDescriptorForBufferPosition(range.start).getScopesArray())
}

isScopeCommentedOrString (scopesArray) {
isScopeCommentedOrString(scopesArray) {
for (let scope of scopesArray.reverse()) {
scope = scope.split('.')
if (scope.includes('embedded') && scope.includes('source')) return false
Expand All @@ -579,7 +579,7 @@ class BracketMatcherView {
return false
}

hasSyntaxTree () {
hasSyntaxTree() {
return this.editor.buffer.getLanguageMode().getSyntaxNodeAtPosition
}
}
24 changes: 19 additions & 5 deletions packages/language-c/grammars/tree-sitter-c/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@
(primitive_type) @storage.type.builtin.c
(type_identifier) @storage.type.other.c

; These types are all reserved words; if we see an identifier with this name,
; it must be a type.
((identifier) @storage.type.builtin.c
(#match? @storage.type.builtin.c "^(char|int|float|double|long)$"))

; Assume any identifier that ends in `_t` is a type. This convention is not
; always followed, but it's a very strong indicator when it's present.
((identifier) @storage.type.other.c
(#match? @storage.type.other.c "_t$"))

[
"enum"
"long"
Expand All @@ -70,6 +80,10 @@
((primitive_type) @support.type.stdint.c
(#match? @support.type.stdint.c "^(int8_t|int16_t|int32_t|int64_t|uint8_t|uint16_t|uint32_t|uint64_t|int_least8_t|int_least16_t|int_least32_t|int_least64_t|uint_least8_t|uint_least16_t|uint_least32_t|uint_least64_t|int_fast8_t|int_fast16_t|int_fast32_t|int_fast64_t|uint_fast8_t|uint_fast16_t|uint_fast32_t|uint_fast64_t|intptr_t|uintptr_t|intmax_t|intmax_t|uintmax_t|uintmax_t)$"))

(enum_specifier
name: (type_identifier) @variable.other.declaration.type.c)
(type_definition
declarator: (_) @variable.other.declaration.type.c)

; CAVEAT: tree-sitter-c doesn't identify placeholders like `%c` in strings.
; Candidate for an injection grammar.
Expand Down Expand Up @@ -111,19 +125,19 @@
declarator: (identifier) @variable.declaration.c)

(field_declaration
(field_identifier) @variable.declaration.c)
(field_identifier) @entity.other.attribute-name.c)

(field_declaration
(pointer_declarator
(field_identifier) @variable.declaration.c))
(field_identifier) @entity.other.attribute-name.c))

(field_declaration
(array_declarator
(field_identifier) @variable.declaration.c))
(field_identifier) @entity.other.attribute-name.c))

(init_declarator
(pointer_declarator
(identifier) @variable.declaration.c))
(identifier) @entity.other.attribute-name.c))

(assignment_expression
left: (identifier) @variable.other.assignment.c)
Expand Down Expand Up @@ -158,7 +172,7 @@
; The "size" in `finfo->size`.
(field_expression
"->"
field: (field_identifier) @variable.other.member.c)
field: (field_identifier) @support.other.property.c)


; FUNCTIONS
Expand Down
1 change: 0 additions & 1 deletion packages/language-c/grammars/tree-sitter-c/locals.scm

This file was deleted.

3 changes: 2 additions & 1 deletion packages/language-c/grammars/tree-sitter-c/tags.scm
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@

((function_declarator (identifier) @name))
(function_declarator
(identifier) @name) @definition.function
11 changes: 11 additions & 0 deletions packages/language-c/grammars/tree-sitter-cpp/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@
(type_identifier) @storage.type.other.cpp
; (struct_specifier) @storage.type.cpp

; These types are all reserved words; if we see an identifier with this name,
; it must be a type.
((identifier) @storage.type.builtin.cpp
(#match? @storage.type.builtin.cpp "^(char|int|float|double|long)$"))

; Assume any identifier that ends in `_t` is a type. This convention is not
; always followed, but it's a very strong indicator when it's present.
((identifier) @storage.type.other.cpp
(#match? @storage.type.other.cpp "_t$"))


[
"enum"
"long"
Expand Down
1 change: 0 additions & 1 deletion packages/language-c/grammars/tree-sitter-cpp/locals.scm

This file was deleted.

16 changes: 15 additions & 1 deletion packages/language-c/grammars/tree-sitter-cpp/tags.scm
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@

((function_declarator (identifier) @name))
(function_declarator
(identifier) @name) @definition.function

(function_declarator
declarator: (qualified_identifier) @name) @definition.function

(class_specifier
name: (type_identifier) @name) @definition.class

(class_specifier
body: (field_declaration_list
(function_definition
declarator: (function_declarator
declarator: (field_identifier) @name)) @definition.method)
(#set! symbol.contextNode "parent.parent.parent.parent.firstNamedChild"))
Loading

0 comments on commit 69038a1

Please sign in to comment.