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

LSP delete code before save buffer unexpectedly if there exists syntax error when use crystallize as LSP server. #41

Open
zw963 opened this issue Jun 10, 2022 · 7 comments
Labels
bug Something isn't working

Comments

@zw963
Copy link

zw963 commented Jun 10, 2022

Following is the log come from emacs lsp client, thank you.

[Trace - 09:36:53 PM] Sending notification 'textDocument/didChange'.
Params: {
  "textDocument": {
    "uri": "file:///home/zw963/Magpie/clover/src/pages/home/index_page.cr",
    "version": 737
  },
  "contentChanges": [
    {
      "range": {
        "start": {
          "line": 2,
          "character": 0
        },
        "end": {
          "line": 2,
          "character": 2
        }
      },
      "rangeLength": 2,
      "text": ""
    }
  ]
}


[Trace - 09:36:53 PM] Sending request 'textDocument/formatting - (1879)'.
Params: {
  "textDocument": {
    "uri": "file:///home/zw963/Magpie/clover/src/pages/home/index_page.cr"
  },
  "options": {
    "tabSize": 2,
    "insertSpaces": true
  }
}


[Trace - 09:36:53 PM] Received response 'textDocument/formatting - (1879)' in 0ms.
Result: [
  {
    "range": {
      "start": {
        "line": 0,
        "character": 0
      },
      "end": {
        "line": 18,
        "character": 0
      }
    },
    "newText": "class Home::IndexPage < AuthLayout\n  def content\n    div class: \"px-4 py-5 my-5 text-center\" do\n      h1 \"CoverApp\", class: \"display-5 fw-bold\"\n      div class: \"col-lg-6 mx-auto\" do\n        para \"It's your lucky day! See a fortune, and share the luck.\", class: \"lead mb-4\"\n        div class: \"d-grid gap-2 d-sm-flex justify-content-sm-center\" do\n          link \"Join\", to: SignUps::New, class: \"btn btn-primary btn-lg px4 me-sm-3\"\n          link \"Login\", to: SignIns::New, class: \"btn btn-outline-secondary btn-lg px-4\"\n        end\n      end\n    end\n    div class: \"container\" do\n      # we will use this later\n    end\n  end\nend\n"
  }
]


[Trace - 09:36:53 PM] Sending notification 'textDocument/didChange'.
Params: {
  "textDocument": {
    "uri": "file:///home/zw963/Magpie/clover/src/pages/home/index_page.cr",
    "version": 738
  },
  "contentChanges": [
    {
      "range": {
        "start": {
          "line": 0,
          "character": 0
        },
        "end": {
          "line": 18,
          "character": 0
        }
      },
      "rangeLength": 659,
      "text": "class Home::IndexPage < AuthLayout\n  def content\n    div class: \"px-4 py-5 my-5 text-center\" do\n      h1 \"CoverApp\", class: \"display-5 fw-bold\"\n      div class: \"col-lg-6 mx-auto\" do\n        para \"It's your lucky day! See a fortune, and share the luck.\", class: \"lead mb-4\"\n        div class: \"d-grid gap-2 d-sm-flex justify-content-sm-center\" do\n          link \"Join\", to: SignUps::New, class: \"btn btn-primary btn-lg px4 me-sm-3\"\n          link \"Login\", to: SignIns::New, class: \"btn btn-outline-secondary btn-lg px-4\"\n        end\n      end\n    end\n    div class: \"container\" do\n      # we will use this later\n    end\n  end\nend\n"
    }
  ]
}


[Trace - 09:36:53 PM] Sending notification 'textDocument/didSave'.
Params: {
  "textDocument": {
    "uri": "file:///home/zw963/Magpie/clover/src/pages/home/index_page.cr",
    "version": 738
  }
}


[Trace - 09:36:53 PM] Sending notification 'textDocument/didChange'.
Params: {
  "textDocument": {
    "uri": "file:///home/zw963/Magpie/clover/src/pages/home/index_page.cr",
    "version": 739
  },
  "contentChanges": [
    {
      "range": {
        "start": {
          "line": 16,
          "character": 3
        },
        "end": {
          "line": 16,
          "character": 3
        }
      },
      "rangeLength": 0,
      "text": "  "
    }
  ]
}


[Trace - 09:36:53 PM] Received request 'window/workDoneProgress/create - (0).
Params: {
  "token": "workspace/compile/284"
}


[Trace - 09:36:53 PM] Sending response 'window/workDoneProgress/create - (0)'. Processing request took 0ms
Params: {
  "jsonrpc": "2.0",
  "id": 0,
  "result": null
}


[Trace - 09:36:53 PM] Received notification '$/progress'.
Params: {
  "token": "workspace/compile/284",
  "value": {
    "title": "Building",
    "message": "/home/zw963/Magpie/clover/src/pages/home/index_page.cr",
    "kind": "begin"
  }
}


[Trace - 09:36:53 PM] Received notification 'textDocument/publishDiagnostics'.
Params: {
  "uri": "file:///home/zw963/Magpie/clover/src/pages/home/index_page.cr",
  "diagnostics": [
    {
      "range": {
        "start": {
          "line": 17,
          "character": 0
        },
        "end": {
          "line": 17,
          "character": 0
        }
      },
      "severity": 1,
      "source": "/home/zw963/Magpie/clover/src/pages/home/index_page.cr",
      "message": "expecting token 'EOF', not 'end'",
      "relatedInformation": []
    }
  ]
}


[Trace - 09:36:53 PM] Received notification '$/progress'.
Params: {
  "token": "workspace/compile/284",
  "value": {
    "message": "Completed with errors.",
    "kind": "end"
  }
}


[Trace - 09:36:54 PM] Sending request 'textDocument/documentSymbol - (1880)'.
Params: {
  "textDocument": {
    "uri": "file:///home/zw963/Magpie/clover/src/pages/home/index_page.cr"
  }
}


[Trace - 09:36:54 PM] Received response 'textDocument/documentSymbol - (1880)' in 382ms.
Result: [
  {
    "name": "Home::IndexPage",
    "kind": 5,
    "range": {
      "start": {
        "line": 0,
        "character": 0
      },
      "end": {
        "line": 16,
        "character": 2
      }
    },
    "selectionRange": {
      "start": {
        "line": 0,
        "character": 0
      },
      "end": {
        "line": 16,
        "character": 2
      }
    },
    "children": [
      {
        "name": "content",
        "detail": "public content ",
        "kind": 12,
        "range": {
          "start": {
            "line": 1,
            "character": 2
          },
          "end": {
            "line": 15,
            "character": 4
          }
        },
        "selectionRange": {
          "start": {
            "line": 1,
            "character": 2
          },
          "end": {
            "line": 15,
            "character": 4
          }
        },
        "children": []
      }
    ]
  }
]
@zw963
Copy link
Author

zw963 commented Jun 26, 2022

BTW, in some cases(possible syntax error too), LSP will always revert my changes whens i save changes.

@peterhoeg
Copy link

I have noticed this too and have had to disable crystalline as a consequence. I haven't been able to find a pattern/reliable way to reproduce it.

@elbywan elbywan added the bug Something isn't working label Jun 7, 2023
@watzon
Copy link

watzon commented Aug 9, 2023

This is most certainly one of the most annoying issues I've faced recently

@elbywan
Copy link
Owner

elbywan commented Aug 9, 2023

I am not facing this issue on mac with vscode (or very rarely).

I am also not planning to work on crystalline anytime soon, so PRs are welcome if anyone is up for it.

@watzon
Copy link

watzon commented Aug 9, 2023

The issue @zw963 and I are having with it undoing the changes you make on save seem to be related to the formatter. Turning off formatting on save would probably work as a temporary solution (if I can figure that one out in neovim).

@zw963
Copy link
Author

zw963 commented Aug 11, 2023

I am not using LSP with Crystal for a long time ...

@DanielGilchrist
Copy link

DanielGilchrist commented Sep 28, 2024

If anyone comes across this issue using neovim, you can configure nvim-lspconfig to disable format on save for crystalline which solves the problem for me

Example for LazyVim:

return {
  "neovim/nvim-lspconfig",
  opts = {
    setup = {
      crystalline = function(_, opts)
        opts.on_attach = function(client)
          client.server_capabilities.documentFormattingProvider = false
          client.server_capabilities.documentRangeFormattingProvider = false
        end
      end,
    },
  },
}

If you're not using LazyVim you should be able to do the same like so:

local lspconfig = require('lspconfig')

lspconfig.crystalline.setup {
  on_attach = function(client, _bufnr)
    client.server_capabilities.documentFormattingProvider = false
    client.server_capabilities.documentRangeFormattingProvider = false
  end
}

I'm personally using LazyVim so I haven't tested the second example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants