This repository was archived by the owner on Aug 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 141
[WIP]: Sheldon diagnostics #1148
Draft
vkatsuba
wants to merge
4
commits into
erlang-ls:main
Choose a base branch
from
vkatsuba:sheldon_diagnostics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -29,3 +29,4 @@ doc | |
| # Emacs temporary files | ||
| .#* | ||
| *# | ||
| rebar3 | ||
4 changes: 4 additions & 0 deletions
4
apps/els_lsp/priv/code_navigation/src/diagnostics_sheldon.erl
This file contains hidden or 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,4 @@ | ||
| %% Trigger some "somestrange" warnings | ||
|
|
||
| %% Trigger some "sheldon" warnings | ||
| -module(diagnostics_sheldon). |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -15,7 +15,8 @@ | |
| getopt, | ||
| erlfmt, | ||
| els_core, | ||
| gradualizer | ||
| gradualizer, | ||
| rebar3_sheldon | ||
| ]}, | ||
| {env, []}, | ||
| {modules, []}, | ||
|
|
||
This file contains hidden or 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,124 @@ | ||
| %%============================================================================== | ||
| %% Sheldon diagnostics | ||
| %%============================================================================== | ||
|
|
||
| -module(els_sheldon_diagnostics). | ||
|
|
||
| %%============================================================================== | ||
| %% Behaviours | ||
| %%============================================================================== | ||
|
|
||
| -behaviour(els_diagnostics). | ||
|
|
||
| %%============================================================================== | ||
| %% Exports | ||
| %%============================================================================== | ||
|
|
||
| -export([ init/0 | ||
| , is_default/0 | ||
| , run/1 | ||
| , source/0 | ||
| ]). | ||
|
|
||
| %%============================================================================== | ||
| %% Includes | ||
| %%============================================================================== | ||
|
|
||
| -include("els_lsp.hrl"). | ||
| -include_lib("kernel/include/logger.hrl"). | ||
|
|
||
| %%============================================================================== | ||
| %% Callback Functions | ||
| %%============================================================================== | ||
|
|
||
| -spec init() -> ok. | ||
| init() -> | ||
| %% By default "sheldon" is not started by reason that he spend few seconds | ||
| %% to prepare and load dictionary. The "sheldon" shoulld be load only once | ||
| %% when diagnostic is enabled. | ||
| application:ensure_all_started(sheldon), | ||
vkatsuba marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ok. | ||
|
|
||
| -spec is_default() -> boolean(). | ||
| is_default() -> | ||
| false. | ||
|
|
||
| -spec run(uri()) -> [els_diagnostics:diagnostic()]. | ||
| run(Uri) -> | ||
| case els_utils:project_relative(Uri) of | ||
| {error, not_relative} -> []; | ||
| RelFile -> | ||
| try | ||
| RegEx = "[_@./#&+-=*]", | ||
vkatsuba marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| rebar3_sheldon_ast:spellcheck([RelFile], RegEx) | ||
| of | ||
| [] -> []; | ||
| Problems -> format_diagnostics(Problems) | ||
| catch Type:Error:Stacktrace -> | ||
| ?LOG_WARNING( "Sheldon error: [type=~p] [error=~p] [stacktrace=~p]" | ||
| , [Type, Error, Stacktrace] | ||
| ), | ||
| [] | ||
| end | ||
| end. | ||
|
|
||
| -spec source() -> binary(). | ||
| source() -> | ||
| <<"Sheldon">>. | ||
|
|
||
| %%============================================================================== | ||
| %% Internal Functions | ||
| %%============================================================================== | ||
|
|
||
| -spec format_diagnostics(any()) -> [map()]. | ||
| format_diagnostics(Data) -> | ||
| R = format_rules(Data), | ||
| lists:flatten(R). | ||
|
|
||
| -spec format_rules([any()]) -> [[map()]]. | ||
| format_rules([]) -> | ||
| []; | ||
| format_rules([#{reason := #{misspelled_words := Miss}} = Data | EItems]) -> | ||
| ItemDiags = format_item(Miss, Data), | ||
| [ItemDiags | format_rules(EItems)]. | ||
|
|
||
| -spec format_item([map()], map()) -> [map()]. | ||
| format_item([#{candidates := [], word := Word} | Items], Data) -> | ||
| #{line := Line, type := Type} = Data, | ||
| Msg = format_text("The word ~p in ~p is unknown.", [Word, Type]), | ||
| Diagnostic = diagnostic(Msg, Line, ?DIAGNOSTIC_WARNING), | ||
| [Diagnostic | format_item(Items, Data)]; | ||
| format_item([#{candidates := Candidates, word := Word} | Items], Data) -> | ||
| #{line := Line, type := Type} = Data, | ||
| FormatCandidates = format_sheldon_candidates(Candidates, []), | ||
| Text = "The word ~p in ~p is unknown. Maybe you wanted to use ~ts?", | ||
| Msg = format_text(Text, [Word, Type, FormatCandidates]), | ||
| Diagnostic = diagnostic(Msg, Line, ?DIAGNOSTIC_WARNING), | ||
| [Diagnostic | format_item(Items, Data)]; | ||
| format_item([], _) -> | ||
| []. | ||
|
|
||
| -spec diagnostic( any(), integer(), els_diagnostics:severity()) -> map(). | ||
| diagnostic(Msg, Ln, Severity) -> | ||
| Range = els_protocol:range(#{from => {Ln, 1}, to => {Ln + 1, 1}}), | ||
| Message = els_utils:to_binary(Msg), | ||
| #{ range => Range | ||
| , severity => Severity | ||
| , code => spellcheck | ||
| , source => source() | ||
| , message => Message | ||
| , relatedInformation => [] | ||
| }. | ||
|
|
||
| -spec format_sheldon_candidates([any()], [[[any()] | char()]]) -> list(). | ||
| format_sheldon_candidates([], Acc) -> | ||
| Acc; | ||
| format_sheldon_candidates([Candidate], Acc) -> | ||
| [Acc, format_text("~p", [Candidate])]; | ||
| format_sheldon_candidates([Candidate | T], Acc) -> | ||
| format_sheldon_candidates(T, [Acc, format_text("~p or ", [Candidate])]). | ||
|
|
||
| -spec format_text(string(), list()) -> string(). | ||
| format_text(Text, Args) -> | ||
| Formatted = io_lib:format(Text, Args), | ||
| unicode:characters_to_list(Formatted). | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.