-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix highlighting of package and project names in variable references.
There are ambiguities in variable references (such as Foo'Bar) where it's ambiguous whether this is a reference to attribute Bar in package Foo of the current project or a reference to a top-level attribute Bar in project Foo. Another ambiguity is Foo.X'Bar, as it's unclear if X is a package in project Foo with attribute Bar or if Foo.X is a child project and Bar is a top-level attribute of the child project. This issue applies to variable references as well the variable attribute references demonstrated above, since variables can be declared both at the project level as well as within a package. These ambiguities can't be resolved by the syntax tree itself and would require active checking of package declaration names in the file or project references (with, extends, etc.) or maintaining a list of known package names. If we can't correctly identify package names in variable references, that leaves a highlighting inconsistency where package declaration names would be highlighted, but variable references wouldn't. This would mean that for consistency, all package name highlighting would need to be removed. None of these options are ideal. Package name highlighting gives visual cues to the reader, indicating if a variable attribute reference is for a package or a project. Since it seems useful for the user to distinguish this information, the option to maintain a list of known packages is implemented. This list is a user option which contains the currently known package names, however if custom packages are created, this list can easily be updated.
- Loading branch information
Showing
5 changed files
with
133 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
;;; gpr-ts-mode.el --- Major mode for GNAT project files using Tree-Sitter -*- lexical-binding: t; -*- | ||
|
||
;; Copyright (C) 2023 Troy Brown | ||
;; Copyright (C) 2023-2024 Troy Brown | ||
|
||
;; Author: Troy Brown <[email protected]> | ||
;; Created: February 2023 | ||
;; Version: 0.5.4 | ||
;; Version: 0.5.5 | ||
;; Keywords: gpr gnat ada languages tree-sitter | ||
;; URL: https://github.com/brownts/gpr-ts-mode | ||
;; Package-Requires: ((emacs "29.1")) | ||
|
@@ -111,6 +111,17 @@ specified. See `treesit-language-source-alist' for full details." | |
:link '(custom-manual :tag "Grammar Installation" "(gpr-ts-mode)Grammar Installation") | ||
:package-version "0.5.0") | ||
|
||
(defcustom gpr-ts-mode-package-names | ||
'("binder" "builder" "check" "clean" "compiler" "cross_reference" | ||
"documentation" "eliminate" "finder" "gnatls" "gnatstub" | ||
"ide" "install" "linker" "metrics" "naming" "pretty_printer" | ||
"remote" "stack" "synchronize") | ||
"List of known package names." | ||
:type '(repeat string) | ||
:group 'gpr-ts | ||
:link '(custom-manual :tag "Syntax Highlighting" "(gpr-ts-mode)Syntax Highlighting") | ||
:package-version "0.5.5") | ||
|
||
(defvar gpr-ts-mode-syntax-table | ||
(let ((table (make-syntax-table))) | ||
(modify-syntax-entry ?- ". 12" table) | ||
|
@@ -472,7 +483,8 @@ Return nil if no child of that type is found." | |
'((package_declaration | ||
[ origname: (name (identifier) @font-lock-function-call-face :anchor) | ||
basename: (name (identifier) @font-lock-function-call-face :anchor)]) | ||
(variable_reference (name (identifier) @font-lock-function-call-face :anchor) "'")) | ||
((variable_reference (name (identifier) @font-lock-function-call-face)) | ||
(:pred gpr-ts-mode--package-name-p @font-lock-function-call-face))) | ||
|
||
;; String literals | ||
:language 'gpr | ||
|
@@ -487,6 +499,7 @@ Return nil if no child of that type is found." | |
;; Variables | ||
:language 'gpr | ||
:feature 'variable | ||
:override t | ||
'((variable_reference (name (identifier) @font-lock-variable-use-face :anchor) :anchor)) | ||
|
||
;; Operators | ||
|
@@ -502,6 +515,13 @@ Return nil if no child of that type is found." | |
|
||
"Font-lock settings for `gpr-ts-mode'.") | ||
|
||
(defun gpr-ts-mode--package-name-p (node) | ||
"Check if NODE identifier matches a known package name." | ||
(let ((identifier (downcase (treesit-node-text node t))) | ||
(packages (mapcar #'downcase gpr-ts-mode-package-names))) | ||
(seq-find (apply-partially #'string-equal identifier) packages))) | ||
|
||
|
||
;;; Imenu | ||
|
||
(defun gpr-ts-mode--defun-name (node) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
with "shared"; | ||
|
||
project Test is | ||
|
||
for Source_Dirs use Shared'Source_Dirs & | ||
-- <- nil | ||
-- ^ font-lock-property-use-face | ||
Test'Source_Dirs & | ||
-- <- nil | ||
-- ^ font-lock-property-use-face | ||
Project'Source_Dirs; | ||
-- ^ nil | ||
-- ^ font-lock-property-use-face | ||
|
||
package Compiler is | ||
for Default_Switches ("Ada") use Compiler'Default_Switches ("Ada") & ("-gnata"); | ||
-- ^ font-lock-function-call-face | ||
-- ^ font-lock-property-use-face | ||
|
||
for Default_Switches ("Ada") use Shared.Compiler'Default_Switches ("Ada"); | ||
-- ^ nil | ||
-- ^ font-lock-function-call-face | ||
-- ^ font-lock-property-use-face | ||
|
||
for Default_Switches ("Ada") use Shared.Child.Compiler'Default_Switches ("Ada"); | ||
-- ^ ^ nil | ||
-- ^ font-lock-function-call-face | ||
-- ^ font-lock-property-use-face | ||
|
||
for Default_Switches ("Ada") use Compiler.My_Switches; | ||
-- ^ font-lock-function-call-face | ||
-- ^ font-lock-variable-use-face | ||
|
||
for Default_Switches ("Ada") use Shared.My_Switches; | ||
-- ^ nil | ||
-- ^ font-lock-variable-use-face | ||
|
||
for Default_Switches ("Ada") use Shared.Compiler.My_Switches; | ||
-- ^ nil | ||
-- ^ font-lock-function-call-face | ||
-- ^ font-lock-variable-use-face | ||
|
||
for Default_Switches ("Ada") use Shared.Child.Compiler.My_Switches; | ||
-- ^ ^ nil | ||
-- ^ font-lock-function-call-face | ||
-- ^ font-lock-variable-use-face | ||
end Compiler; | ||
|
||
end Test; |