Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement]: KCL Diagnostic #634

Closed
Tracked by #610
He1pa opened this issue Aug 1, 2023 · 0 comments
Closed
Tracked by #610

[Enhancement]: KCL Diagnostic #634

He1pa opened this issue Aug 1, 2023 · 0 comments
Assignees
Labels
enhancement New feature or request error-handling Issues or PRs related to kcl error handling ide Issues or PRs related to kcl LSP and IDE plugins

Comments

@He1pa
Copy link
Contributor

He1pa commented Aug 1, 2023

Enhancement

Diagnostic postion

LSP Diagnostic position is a range(start and end) position but in KCL is a single (start) postion.

LSP DIagnostic: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#diagnostic
KCL Diagnostic position: https://github.com/kcl-lang/kcl/blob/82d149c36736fc4cae710f33ecf775a5766561e6/kclvm/error/src/diagnostic.rs

This will affect the display of diagnostics in the IDE, as well as the development of some features.

Multiple positions diagnostic

For some errors that have multiple locations, e.g. attr confict, the error message is scattered across different position. Incomplete error messages at a single position make it hard to understand

Edit message

Steps:

  1. Client: open/edit file.
  2. Server: generate and publish LSP Diagnostics(message, range, code(number or string), related messages and some information) from KCL Diagnostics.
  3. Client: click on quick fix button and send CodeAction request, the param contains LSP Diagnostics generated in step 2.
  4. Server: generate code action(TextEdit(range, newtext) to replace the diagnostic source code) and response to client.

LSP CodeAction(quick fix) use some edit message to replace source code in diagnostic position.

Rely on diagnostic unique ID/code to provide edit message

pub(crate) fn handle_code_action(
    _snap: LanguageServerSnapshot,
    params: lsp_types::CodeActionParams,
    _sender: Sender<Task>,
) -> anyhow::Result<Option<lsp_types::CodeActionResponse>> {
    let diags = params.context.diagnostics;
    let mut code_actions: Vec<lsp_types::CodeActionOrCommand> = vec![];
    for diag in diags {
        match diag.code.into_kcl_diag_code() {
            code::UnusedImport => {
                let mut changes = HashMap::new();
                changes.insert(
                    params.text_document.uri.clone(),
                    vec![TextEdit {
                        range: diag.range,
                        new_text: "".to_string(),
                    }],
                );
                code_actions.push(CodeActionOrCommand::CodeAction(CodeAction {
                    title: "Unused import".to_string(),
                    kind: Some(CodeActionKind::QUICKFIX),
                    diagnostics: Some(vec![diag.clone()]),
                    edit: Some(lsp_types::WorkspaceEdit {
                        changes: Some(changes),
                        ..Default::default()
                    }),
                    ..Default::default()
                }))
            }
            code::ReImport => {
                let mut changes = HashMap::new();
                changes.insert(
                    params.text_document.uri.clone(),
                    vec![TextEdit {
                        range: diag.range,
                        new_text: "".to_string(),
                    }],
                );
                code_actions.push(CodeActionOrCommand::CodeAction(CodeAction {
                    title: "Unused import".to_string(),
                    kind: Some(CodeActionKind::QUICKFIX),
                    diagnostics: Some(vec![diag.clone()]),
                    edit: Some(lsp_types::WorkspaceEdit {
                        changes: Some(changes),
                        ..Default::default()
                    }),
                    ..Default::default()
                }))
            }
            ...
        }
    }
    Ok(Some(code_actions))
}
@He1pa He1pa changed the title Enhancement: KCL Diagnostic [Enhancement]: KCL Diagnostic Aug 1, 2023
@He1pa He1pa added ide Issues or PRs related to kcl LSP and IDE plugins error-handling Issues or PRs related to kcl error handling labels Aug 2, 2023
@He1pa He1pa self-assigned this Aug 2, 2023
@He1pa He1pa added the enhancement New feature or request label Aug 2, 2023
@He1pa He1pa mentioned this issue Aug 22, 2023
16 tasks
@He1pa He1pa closed this as completed Aug 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request error-handling Issues or PRs related to kcl error handling ide Issues or PRs related to kcl LSP and IDE plugins
Projects
None yet
Development

No branches or pull requests

1 participant