Giter Club home page Giter Club logo

Comments (8)

predragnikolic avatar predragnikolic commented on June 26, 2024 1

It is a bug.

We trigger multiple lsp_apply_document_edit commands at the same time, here ->

LSP/plugin/code_actions.py

Lines 261 to 265 in cd7cb68

for config_name, code_actions in responses:
session = self._task_runner.session_by_name(config_name, 'codeActionProvider')
if session:
tasks.extend([
session.run_code_action_async(action, progress=False, view=view) for action in code_actions

So when the edits for the first one is applied,
the second one will fail with the error LSP: ignoring edit due to non-matching document version ,
because the view.count changed.

if required_view_version is not None and required_view_version != view_version:

Request
{
  "context": {
    "diagnostics": [
      {
        "code": 2304,
        "message": "Cannot find name 'useState'.",
        "range": {"end": {"character": 20, "line": 18 }, "start": {"character": 12, "line": 18 } },
        "severity": 1,
        "source": "typescript",
        "tags": []
      },
      {
        "code": 6133,
        "message": "'d' is declared but its value is never read.",
        "range": {"end": {"character": 9, "line": 18 }, "start": {"character": 8, "line": 18 } },
        "severity": 4,
        "source": "typescript",
        "tags": [1]
      }
    ],
    "only": [
      "source.addMissingImports.ts",
      "source.organizeImports.ts"
    ],
    "triggerKind": 2
  },
  "range": {"end": {"character": 0, "line": 113 }, "start": {"character": 0, "line": 0 } },
  "textDocument": {
    "uri": "file:///Users/predrag/Documents/work/pravoslavnaknjizara/appAdmin/src/pages/OrdersPage/OrdersPage.tsx"
  }
}
Response
[
  {
    "edit": {
      "documentChanges": [
        {
          "edits": [
            {
              "newText": "import { DataNotFound } from \"@/components/NotDataFound/NotDataFound\"\nimport { SomethingBadHappen } from \"@/components/SomethingBadHappen/SomethingBadHappen\"\nimport { TopBar } from \"@/components/TopBar/TopBar\"\nimport { fets } from \"@/http\"\nimport { orderStatusToLabel } from \"@/utils/orderStatusLabels\"\nimport { useScrollToTop } from \"@/utils/useScrollToTop\"\nimport { Input, Pagination, ScrollShadow, Tab, Tabs } from \"@nextui-org/react\"\nimport { OrderStatus } from \"@shared/utils/applicationWideConstants\"\nimport { useDebounce } from \"@shared/utils/hooks/useDebounce\"\nimport { useNoInitialEffect } from \"@shared/utils/hooks/useNoInitialEffect\"\nimport { usePagination } from \"@shared/utils/hooks/usePagination\"\nimport { useQueryParam } from \"@shared/utils/hooks/useQueryParams\"\nimport { keepPreviousData, useQuery } from \"@tanstack/react-query\"\nimport { AnimatePresence, motion } from \"framer-motion\"\nimport { Search } from \"lucide-react\"\nimport { OrderItem } from \"./components/OrderItem\"\n",
              "range": {
                "end": {
                  "character": 0,
                  "line": 1
                },
                "start": {
                  "character": 0,
                  "line": 0
                }
              }
            },
            {
              "newText": "",
              "range": {"end": {"character": 0, "line": 2 }, "start": {"character": 0, "line": 1 } } },
            {
              "newText": "",
              "range": {"end": {"character": 0, "line": 3 }, "start": {"character": 0, "line": 2 }
              }
            },
            {
              "newText": "",
              "range": {"end": {"character": 0, "line": 4 }, "start": {"character": 0, "line": 3 } }
            },
            {
              "newText": "",
              "range": {"end": {"character": 0, "line": 5 }, "start": {"character": 0, "line": 4 }
              }
            },
            {
              "newText": "",
              "range": {"end": {"character": 0, "line": 6 }, "start": {"character": 0, "line": 5 }
              }
            },
            {
              "newText": "",
              "range": {"end": {"character": 0, "line": 7 }, "start": {"character": 0, "line": 6 } }
            },
            {
              "newText": "",
              "range": {"end": {"character": 0, "line": 8 }, "start": {"character": 0, "line": 7 } }
            },
            {
              "newText": "",
              "range": {"end": {"character": 0, "line": 9 }, "start": {"character": 0, "line": 8 } }
            },
            {
              "newText": "",
              "range": {"end": {"character": 0, "line": 10 }, "start": {"character": 0, "line": 9 } }
            },
            {
              "newText": "",
              "range": {"end": {"character": 0, "line": 11 }, "start": {"character": 0, "line": 10 } }
            },
            {
              "newText": "",
              "range": {"end": {"character": 0, "line": 12 }, "start": {"character": 0, "line": 11 } }
            },
            {
              "newText": "",
              "range": {"end": {"character": 0, "line": 13 }, "start": {"character": 0, "line": 12 } }
            },
            {
              "newText": "",
              "range": {"end": {"character": 0, "line": 14 }, "start": {"character": 0, "line": 13 } }
            },
            {
              "newText": "",
              "range": {"end": {"character": 0, "line": 15 }, "start": {"character": 0, "line": 14 } }
            },
            {
              "newText": "",
              "range": {"end": {"character": 0, "line": 16 }, "start": {"character": 0, "line": 15 } }
            }
          ],
          "textDocument": {
            "uri": "file:///Users/predrag/Documents/work/pravoslavnaknjizara/appAdmin/src/pages/OrdersPage/OrdersPage.tsx",
            "version": 192
          }
        }
      ]
    },
    "kind": "source.organizeImports.ts",
    "title": "Organize Imports"
  },
  {
    "edit": {
      "documentChanges": [
        {
          "edits": [
            {
              "newText": "import { useState } from \"react\"\n",
              "range": {"end": {"character": 0, "line": 16 }, "start": {"character": 0, "line": 16 } }
            }
          ],
          "textDocument": {
            "uri": "file:///Users/predrag/Documents/work/pravoslavnaknjizara/appAdmin/src/pages/OrdersPage/OrdersPage.tsx",
            "version": 192
          }
        }
      ]
    },
    "kind": "source.addMissingImports.ts",
    "title": "Add all missing imports"
  }
]

from lsp.

sricks avatar sricks commented on June 26, 2024

Also noticed something else. If I have source.removeUnused enabled, it only removes some bad imports, in weird ways. In the same App.tsx file:

import { useCallback, useState } from "react";
import reactLogo from "./assets/react.svg";
import viteLogo from "/vite.svg";

useCallback, reactLogo and viteLogo are not needed. I experience the same problem of save-after-typing, even when removeUnused is the only action, but also:

If I use removeUnused AND organizeImports together on save, nothing will get removed at all, no matter what. UNLESS I manually delete the reactLogo and viteLogo import lines; then, the useCallback will get deleted appropriately.

With all of these issues, order of the actions does not seem to matter.

from lsp.

sricks avatar sricks commented on June 26, 2024

I'm not sure if something changed, but I've discovered that addMissingImports never works as long as organizeImports is enabled. I think it might be because the changes from both of these actions are batched; they affect the same lines, so, somehow the end state is that nothing gets changed?

Logs:

:: [19:53:49.360] --> LSP-typescript textDocument/codeAction (17): {'textDocument': {'uri': 'file:///home/d/react/todo/src/App.tsx'}, 'range': {'start': {'line': 0, 'character': 0}, 'end': {'line': 87, 'character': 0}}, 'context': {'diagnostics': [{'range': {'start': {'line': 48, 'character': 4}, 'end': {'line': 48, 'character': 12}}, 'message': "Cannot find name 'TodoList'.", 'severity': 1, 'code': 2304, 'source': 'typescript', 'tags': []}], 'triggerKind': <CodeActionTriggerKind.Automatic: 2>, 'only': ['source.fixAll.ts', 'source.addMissingImports.ts', 'source.organizeImports.ts']}}
:: [19:53:49.360] --> LSP-eslint textDocument/codeAction (2): {'textDocument': {'uri': 'file:///home/d/react/todo/src/App.tsx'}, 'range': {'start': {'line': 0, 'character': 0}, 'end': {'line': 87, 'character': 0}}, 'context': {'diagnostics': [], 'triggerKind': <CodeActionTriggerKind.Automatic: 2>, 'only': ['source.fixAll.eslint']}}
:: [19:53:49.371] <<< LSP-eslint (2) (duration: 10ms): []
:: [19:53:49.411] <<< LSP-typescript (17) (duration: 51ms): [{'title': 'Organize Imports', 'edit': {'documentChanges': [{'textDocument': {'uri': 'file:///home/d/react/todo/src/App.tsx', 'version': 311}, 'edits': [{'range': {'start': {'line': 0, 'character': 0}, 'end': {'line': 1, 'character': 0}}, 'newText': 'import "@/App.css";\nimport Button from "@/components/Button";\nimport FilterButton from "@/components/FilterButton";\nimport SearchBox from "@/components/SearchBox";\nimport TodoFooter from "@/components/TodoFooter";\nimport { Filter, Todo } from "@/types";\nimport { useState } from "react";\n'}, {'range': {'start': {'line': 1, 'character': 0}, 'end': {'line': 2, 'character': 0}}, 'newText': ''}, {'range': {'start': {'line': 2, 'character': 0}, 'end': {'line': 3, 'character': 0}}, 'newText': ''}, {'range': {'start': {'line': 3, 'character': 0}, 'end': {'line': 4, 'character': 0}}, 'newText': ''}, {'range': {'start': {'line': 4, 'character': 0}, 'end': {'line': 5, 'character': 0}}, 'newText': ''}, {'range': {'start': {'line': 5, 'character': 0}, 'end': {'line': 6, 'character': 0}}, 'newText': ''}, {'range': {'start': {'line': 6, 'character': 0}, 'end': {'line': 7, 'character': 0}}, 'newText': ''}]}]}, 'kind': 'source.organizeImports.ts'}, {'title': 'Add all missing imports', 'edit': {'documentChanges': [{'textDocument': {'uri': 'file:///home/d/react/todo/src/App.tsx', 'version': 311}, 'edits': [{'range': {'start': {'line': 5, 'character': 0}, 'end': {'line': 5, 'character': 0}}, 'newText': 'import TodoList from "@/components/TodoList";\n'}]}]}, 'kind': 'source.addMissingImports.ts'}]

from lsp.

sricks avatar sricks commented on June 26, 2024

By the way, the workaround I'm using is

  • Disable organizeImports on save
  • Use the Sublime command pallete to organize imports on-demand
  • Always wait ~1 second after editing before saving to ensure imports are added on save

from lsp.

jwortmann avatar jwortmann commented on June 26, 2024

That looks like a missed bug to me still from the Python 3.8 update. LSP tries to send the enum value object for the triggerKind instead of just the plain value. I guess there is a corresponding error message in the console that it's not JSON-serializable?

from lsp.

sricks avatar sricks commented on June 26, 2024

I enabled "log_debug": true in my LSP settings but the only thing I see in my sublime console on save is:

LSP: ignoring edit due to non-matching document version
LSP: ignoring edit due to non-matching document version

If I can help debug this in some way, please let me know and I'd be glad to help.

from lsp.

rchl avatar rchl commented on June 26, 2024

I believe I said somewhere that vscode only sends at most one code action in the only list per request. If so then the way to fix it should be to trigger as many requests as there are "fix on save code actions" specified.

from lsp.

predragnikolic avatar predragnikolic commented on June 26, 2024

closing as duplicate of #2421

from lsp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.