Skip to content

Commit

Permalink
fix: the incorrect location after file content updates
Browse files Browse the repository at this point in the history
  • Loading branch information
zcf0508 committed Oct 29, 2024
1 parent a9937c9 commit 760aff1
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 120 deletions.
7 changes: 3 additions & 4 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
"build": "tsup"
},
"devDependencies": {
"@volar/language-core": "^2.3.4",
"@volar/source-map": "^2.3.4",
"@volar/typescript": "^2.3.4",
"@vue/language-core": "^2.0.29",
"@volar/language-core": "^2.4.8",
"@volar/typescript": "^2.4.8",
"@vue/language-core": "^2.1.8",
"typescript": "^5.3.0"
},
"dependencies": {
Expand Down
77 changes: 25 additions & 52 deletions packages/api/src/vue.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
/* eslint-disable import/no-extraneous-dependencies */
import type * as ts from "typescript/lib/tsserverlibrary"
import { type Language } from "@volar/language-core"
import {
proxyCreateProgram,
type TypeScriptServiceScript,
type Language,
} from "@volar/language-core"
import { proxyCreateProgram } from "@volar/typescript"
} from "@volar/typescript"
import {
VueCompilerOptions,
createParsedCommandLine,
resolveVueCompilerOptions,
createVueLanguagePlugin2,
createRootFileChecker,
createVueLanguagePlugin,
} from "@vue/language-core"
import { SourceFileLocation, TypescriptContext } from "./types"

Expand All @@ -23,7 +22,7 @@ type VuePrograme = ts.Program & {
__vue__?: { language: Language }
}

let oldPrograme: VuePrograme | undefined
let tsPrograme: VuePrograme | undefined

function getMappingOffset(
language: Language,
Expand Down Expand Up @@ -55,7 +54,7 @@ export function getPositionOfLineAndCharacterForVue(
host: ctx.ts.createCompilerHost(compilerOptions),
rootNames: ctx.program.getRootFileNames(),
options: compilerOptions,
oldProgram: oldPrograme || ctx.program,
oldProgram: ctx.program,
}

let vueOptions: VueCompilerOptions
Expand All @@ -74,32 +73,27 @@ export function getPositionOfLineAndCharacterForVue(
: resolveVueCompilerOptions({
extensions: [".vue", ".cext"],
})
const vueLanguagePlugin = createVueLanguagePlugin2<string>(
const vueLanguagePlugin = createVueLanguagePlugin<string>(
ts,
(id) => id,
createRootFileChecker(
undefined,
() =>
options.rootNames.map((rootName) =>
rootName.replace(windowsPathReg, "/")
),
options.host?.useCaseSensitiveFileNames?.() ?? false
),
options.options,
vueOptions
vueOptions,
(id) => id
)
return [vueLanguagePlugin]
}
)

oldPrograme = oldPrograme ?? ctx.program
tsPrograme = ctx.program

if (!oldPrograme?.__vue__ && !oldPrograme?.__volar__) {
if (!(tsPrograme?.__vue__ || tsPrograme?.__volar__)) {
console.log("create vue program")
oldPrograme = createProgram(options) as VuePrograme
tsPrograme = createProgram(options) as VuePrograme
}

const language = (oldPrograme.__volar__ || oldPrograme.__vue__)?.language
let fixLocation = (startPos: number) =>
undefined as ts.LineAndCharacter | undefined

const language = (tsPrograme.__volar__ || tsPrograme.__vue__)?.language
if (language?.scripts) {
const vFile = language.scripts.get(fileName)
const serviceScript =
Expand All @@ -110,37 +104,16 @@ export function getPositionOfLineAndCharacterForVue(
const sourceMap = language.maps.get(serviceScript.code, vFile)

const snapshotLength = getMappingOffset(language, serviceScript)
if (startPos < snapshotLength) {
for (const [generatedLocation] of sourceMap.toGeneratedLocation(
startPos
)) {
if (generatedLocation) {
startPos = generatedLocation + snapshotLength
}

for (const [generatedLocation] of sourceMap.toGeneratedLocation(
startPos
)) {
if (generatedLocation) {
startPos = generatedLocation + snapshotLength
}
}
}
}

function fixLocation(startPos: number) {
if (!oldPrograme?.__vue__ && !oldPrograme?.__volar__) {
console.log("create vue program")
oldPrograme = createProgram(options) as VuePrograme
}

const language = (oldPrograme.__volar__ || oldPrograme.__vue__)
?.language

if (language?.scripts) {
const vFile = language.scripts.get(fileName)
const serviceScript =
vFile?.generated?.languagePlugin.typescript?.getServiceScript(
vFile.generated.root
)
if (vFile?.generated?.root?.languageId === "vue" && serviceScript) {
const sourceMap = language.maps.get(serviceScript.code, vFile)
const snapshotLength = getMappingOffset(language, serviceScript)

fixLocation = (startPos: number) => {
for (const [sourceLocation] of sourceMap.toSourceLocation(
startPos - snapshotLength
)) {
Expand All @@ -152,10 +125,10 @@ export function getPositionOfLineAndCharacterForVue(
return restoreLocation
}
}

return undefined
}
}

return undefined
}

return [startPos, fixLocation] as const
Expand Down
136 changes: 72 additions & 64 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,21 @@
resolved "https://registry.npmjs.org/@babel/helper-string-parser/-/helper-string-parser-7.19.4.tgz"
integrity sha512-nHtDoQcuqFmwYNYPz3Rah5ph2p8PFeFCsZk9A/48dPc/rGocJ5J3hAAZ7pb76VWX3fZKu+uEr/FhH5jLx7umrw==

"@babel/helper-string-parser@^7.25.9":
version "7.25.9"
resolved "https://registry.yarnpkg.com/@babel/helper-string-parser/-/helper-string-parser-7.25.9.tgz#1aabb72ee72ed35789b4bbcad3ca2862ce614e8c"
integrity sha512-4A/SCr/2KLd5jrtOMFzaKjVtAei3+2r/NChoBNoZ3EyP/+GlhoaEGoWOZUmFmoITP7zOJyHIMm+DYRd8o3PvHA==

"@babel/helper-validator-identifier@^7.18.6", "@babel/helper-validator-identifier@^7.19.1":
version "7.19.1"
resolved "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.19.1.tgz"
integrity sha512-awrNfaMtnHUr653GgGEs++LlAvW6w+DcPrOliSMXWCKo597CwL5Acf/wWdNkf/tfEQE3mjkeD1YOVZOUV/od1w==

"@babel/helper-validator-identifier@^7.25.9":
version "7.25.9"
resolved "https://registry.yarnpkg.com/@babel/helper-validator-identifier/-/helper-validator-identifier-7.25.9.tgz#24b64e2c3ec7cd3b3c547729b8d16871f22cbdc7"
integrity sha512-Ed61U6XJc3CVRfkERJWDz4dJwKe7iLmmJsbOGu9wSloNSFttHV0I8g6UAgb7qnK5ly5bGLPd4oXZlxCdANBOWQ==

"@babel/highlight@^7.18.6":
version "7.18.6"
resolved "https://registry.npmjs.org/@babel/highlight/-/highlight-7.18.6.tgz"
Expand All @@ -40,10 +50,12 @@
chalk "^2.0.0"
js-tokens "^4.0.0"

"@babel/parser@^7.23.9":
version "7.24.0"
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.24.0.tgz#26a3d1ff49031c53a97d03b604375f028746a9ac"
integrity sha512-QuP/FxEAzMSjXygs8v4N9dvdXzEHN4W1oF3PxuWAtPo08UdM17u89RDMgjLn/mlc56iM0HlLmVkO/wgR+rDgHg==
"@babel/parser@^7.25.3":
version "7.26.1"
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.26.1.tgz#44e02499960df2cdce2c456372a3e8e0c3c5c975"
integrity sha512-reoQYNiAJreZNsJzyrDNzFQ+IQ5JFiIzAHJg9bn94S3l+4++J7RsIhNMoB+lgP/9tpmiAQqspv+xfdxTSzREOw==
dependencies:
"@babel/types" "^7.26.0"

"@babel/plugin-syntax-jsx@^7.17.12":
version "7.18.6"
Expand All @@ -68,6 +80,14 @@
"@babel/helper-validator-identifier" "^7.19.1"
to-fast-properties "^2.0.0"

"@babel/types@^7.26.0":
version "7.26.0"
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.26.0.tgz#deabd08d6b753bc8e0f198f8709fb575e31774ff"
integrity sha512-Z/yiTPj+lDVnF7lWeKCIJzaIkI0vYO87dMpZ4bg4TDrFe4XXLFWL1TbXU27gBP3QccxV9mZICCrnjnYlJjXHOA==
dependencies:
"@babel/helper-string-parser" "^7.25.9"
"@babel/helper-validator-identifier" "^7.25.9"

"@cspotcode/source-map-support@^0.8.0":
version "0.8.1"
resolved "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz"
Expand Down Expand Up @@ -2016,36 +2036,24 @@
pathe "^1.1.1"
pretty-format "^29.7.0"

"@volar/language-core@2.3.4", "@volar/language-core@^2.3.4":
version "2.3.4"
resolved "https://registry.yarnpkg.com/@volar/language-core/-/language-core-2.3.4.tgz#51de0263039a567a12a1eea90e02e59cdbf5de3b"
integrity sha512-wXBhY11qG6pCDAqDnbBRFIDSIwbqkWI7no+lj5+L7IlA7HRIjRP7YQLGzT0LF4lS6eHkMSsclXqy9DwYJasZTQ==
dependencies:
"@volar/source-map" "2.3.4"

"@volar/language-core@~2.4.0-alpha.18":
version "2.4.0-alpha.18"
resolved "https://registry.yarnpkg.com/@volar/language-core/-/language-core-2.4.0-alpha.18.tgz#dafffd68ac07c26d69de16741187fd4c06bfa345"
integrity sha512-JAYeJvYQQROmVRtSBIczaPjP3DX4QW1fOqW1Ebs0d3Y3EwSNRglz03dSv0Dm61dzd0Yx3WgTW3hndDnTQqgmyg==
"@volar/language-core@2.4.8", "@volar/language-core@^2.4.8", "@volar/language-core@~2.4.8":
version "2.4.8"
resolved "https://registry.yarnpkg.com/@volar/language-core/-/language-core-2.4.8.tgz#022f9a8f8c0615469d2f8290f9b44528807c99ca"
integrity sha512-K/GxMOXGq997bO00cdFhTNuR85xPxj0BEEAy+BaqqayTmy9Tmhfgmq2wpJcVspRhcwfgPoE2/mEJa26emUhG/g==
dependencies:
"@volar/source-map" "2.4.0-alpha.18"
"@volar/source-map" "2.4.8"

"@volar/source-map@2.3.4", "@volar/source-map@^2.3.4":
version "2.3.4"
resolved "https://registry.yarnpkg.com/@volar/source-map/-/source-map-2.3.4.tgz#1d285610134fe565ca59a54e5a99c12befc70c93"
integrity sha512-C+t63nwcblqLIVTYXaVi/+gC8NukDaDIQI72J3R7aXGvtgaVB16c+J8Iz7/VfOy7kjYv7lf5GhBny6ACw9fTGQ==
"@volar/source-map@2.4.8":
version "2.4.8"
resolved "https://registry.yarnpkg.com/@volar/source-map/-/source-map-2.4.8.tgz#bc699a095aaab405ddc0e2c5a011edc151e787fd"
integrity sha512-jeWJBkC/WivdelMwxKkpFL811uH/jJ1kVxa+c7OvG48DXc3VrP7pplSWPP2W1dLMqBxD+awRlg55FQQfiup4cA==

"@volar/source-map@2.4.0-alpha.18":
version "2.4.0-alpha.18"
resolved "https://registry.yarnpkg.com/@volar/source-map/-/source-map-2.4.0-alpha.18.tgz#a2413932ff6b1821ae8efcbd9249d4da3f99f223"
integrity sha512-MTeCV9MUwwsH0sNFiZwKtFrrVZUK6p8ioZs3xFzHc2cvDXHWlYN3bChdQtwKX+FY2HG6H3CfAu1pKijolzIQ8g==

"@volar/typescript@^2.3.4":
version "2.3.4"
resolved "https://registry.yarnpkg.com/@volar/typescript/-/typescript-2.3.4.tgz#bfa2834c79bd0b9a38cdfdf220fea0afa8ed64b0"
integrity sha512-acCvt7dZECyKcvO5geNybmrqOsu9u8n5XP1rfiYsOLYGPxvHRav9BVmEdRyZ3vvY6mNyQ1wLL5Hday4IShe17w==
"@volar/typescript@^2.4.8":
version "2.4.8"
resolved "https://registry.yarnpkg.com/@volar/typescript/-/typescript-2.4.8.tgz#4cfb90b3226e04d781d48fa519fed0838d7b1504"
integrity sha512-6xkIYJ5xxghVBhVywMoPMidDDAFT1OoQeXwa27HSgJ6AiIKRe61RXLoik+14Z7r0JvnblXVsjsRLmCr42SGzqg==
dependencies:
"@volar/language-core" "2.3.4"
"@volar/language-core" "2.4.8"
path-browserify "^1.0.1"
vscode-uri "^3.0.8"

Expand All @@ -2069,24 +2077,24 @@
jszip "^3.10.1"
semver "^7.5.2"

"@vue/compiler-core@3.4.21":
version "3.4.21"
resolved "https://registry.yarnpkg.com/@vue/compiler-core/-/compiler-core-3.4.21.tgz#868b7085378fc24e58c9aed14c8d62110a62be1a"
integrity sha512-MjXawxZf2SbZszLPYxaFCjxfibYrzr3eYbKxwpLR9EQN+oaziSu3qKVbwBERj1IFIB8OLUewxB5m/BFzi613og==
"@vue/compiler-core@3.5.12":
version "3.5.12"
resolved "https://registry.yarnpkg.com/@vue/compiler-core/-/compiler-core-3.5.12.tgz#bd70b7dabd12b0b6f31bc53418ba3da77994c437"
integrity sha512-ISyBTRMmMYagUxhcpyEH0hpXRd/KqDU4ymofPgl2XAkY9ZhQ+h0ovEZJIiPop13UmR/54oA2cgMDjgroRelaEw==
dependencies:
"@babel/parser" "^7.23.9"
"@vue/shared" "3.4.21"
"@babel/parser" "^7.25.3"
"@vue/shared" "3.5.12"
entities "^4.5.0"
estree-walker "^2.0.2"
source-map-js "^1.0.2"
source-map-js "^1.2.0"

"@vue/compiler-dom@^3.4.0":
version "3.4.21"
resolved "https://registry.yarnpkg.com/@vue/compiler-dom/-/compiler-dom-3.4.21.tgz#0077c355e2008207283a5a87d510330d22546803"
integrity sha512-IZC6FKowtT1sl0CR5DpXSiEB5ayw75oT2bma1BEhV7RRR1+cfwLrxc2Z8Zq/RGFzJ8w5r9QtCOvTjQgdn0IKmA==
"@vue/compiler-dom@^3.5.0":
version "3.5.12"
resolved "https://registry.yarnpkg.com/@vue/compiler-dom/-/compiler-dom-3.5.12.tgz#456d631d11102535b7ee6fd954cf2c93158d0354"
integrity sha512-9G6PbJ03uwxLHKQ3P42cMTi85lDRvGLB2rSGOiQqtXELat6uI4n8cNz9yjfVHRPIu+MsK6TE418Giruvgptckg==
dependencies:
"@vue/compiler-core" "3.4.21"
"@vue/shared" "3.4.21"
"@vue/compiler-core" "3.5.12"
"@vue/shared" "3.5.12"

"@vue/compiler-vue2@^2.7.16":
version "2.7.16"
Expand All @@ -2096,24 +2104,24 @@
de-indent "^1.0.2"
he "^1.2.0"

"@vue/language-core@^2.0.29":
version "2.0.29"
resolved "https://registry.yarnpkg.com/@vue/language-core/-/language-core-2.0.29.tgz#19462d786cd7a1c21dbe575b46970a57094e0357"
integrity sha512-o2qz9JPjhdoVj8D2+9bDXbaI4q2uZTHQA/dbyZT4Bj1FR9viZxDJnLcKVHfxdn6wsOzRgpqIzJEEmSSvgMvDTQ==
"@vue/language-core@^2.1.8":
version "2.1.8"
resolved "https://registry.yarnpkg.com/@vue/language-core/-/language-core-2.1.8.tgz#1bc68852021f7972649c448c44cdaeeb801c2363"
integrity sha512-DtPUKrIRqqzY1joGfVHxHWZoxXZbCQLmVtW+QTifuPInfcs1R/3UAdlJXDp+lpSpP9lI5m+jMYYlwDXXu3KSTg==
dependencies:
"@volar/language-core" "~2.4.0-alpha.18"
"@vue/compiler-dom" "^3.4.0"
"@volar/language-core" "~2.4.8"
"@vue/compiler-dom" "^3.5.0"
"@vue/compiler-vue2" "^2.7.16"
"@vue/shared" "^3.4.0"
computeds "^0.0.1"
"@vue/shared" "^3.5.0"
alien-signals "^0.2.0"
minimatch "^9.0.3"
muggle-string "^0.4.1"
path-browserify "^1.0.1"

"@vue/shared@3.4.21", "@vue/shared@^3.4.0":
version "3.4.21"
resolved "https://registry.yarnpkg.com/@vue/shared/-/shared-3.4.21.tgz#de526a9059d0a599f0b429af7037cd0c3ed7d5a1"
integrity sha512-PuJe7vDIi6VYSinuEbUIQgMIRZGgM8e4R+G+/dQTk0X1NEdvgvvgv7m+rfmDH1gZzyA1OjjoWskvHlfRNfQf3g==
"@vue/shared@3.5.12", "@vue/shared@^3.5.0":
version "3.5.12"
resolved "https://registry.yarnpkg.com/@vue/shared/-/shared-3.5.12.tgz#f9e45b7f63f2c3f40d84237b1194b7f67de192e3"
integrity sha512-L2RPSAwUFbgZH20etwrXyVyCBu9OxRSi8T/38QsvnkJyvq2LufW2lDCOzm7t/U9C1mkhJGWYfCuFBCmIuNivrg==

"@wdio/cli@~8.0.15":
version "8.0.15"
Expand Down Expand Up @@ -2462,6 +2470,11 @@ ajv@^8.0.0, ajv@^8.10.0, ajv@^8.11.0:
require-from-string "^2.0.2"
uri-js "^4.2.2"

alien-signals@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/alien-signals/-/alien-signals-0.2.0.tgz#f159e1b767802f2444fed4e58b8d15fd8bab1c24"
integrity sha512-StlonZhBBrsPPwrDjiPAiVTf/rolxffLxVPT60Qv/t88BZ81BvUVzHgGqEFvJ1ii8HXtm1+zU2Icr59tfWEcag==

ansi-colors@4.1.1:
version "4.1.1"
resolved "https://registry.npmjs.org/ansi-colors/-/ansi-colors-4.1.1.tgz"
Expand Down Expand Up @@ -3479,11 +3492,6 @@ compress-commons@^5.0.1:
normalize-path "^3.0.0"
readable-stream "^3.6.0"

computeds@^0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/computeds/-/computeds-0.0.1.tgz#215b08a4ba3e08a11ff6eee5d6d8d7166a97ce2e"
integrity sha512-7CEBgcMjVmitjYo5q8JTJVra6X5mQ20uTThdK+0kR7UEaDrAWEQcRiBtWJzga4eRpP6afNwwLsX2SET2JhVB1Q==

concat-map@0.0.1:
version "0.0.1"
resolved "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz"
Expand Down Expand Up @@ -9215,10 +9223,10 @@ sort-keys@^4.0.0:
dependencies:
is-plain-obj "^2.0.0"

source-map-js@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/source-map-js/-/source-map-js-1.0.2.tgz#adbc361d9c62df380125e7f161f71c826f1e490c"
integrity sha512-R0XvVJ9WusLiqTCEiGCmICCMplcCkIwwR11mOSD9CR5u+IXYdiseeEuXCVAjS54zqwkLcPNnmU4OeJ6tUrWhDw==
source-map-js@^1.2.0:
version "1.2.1"
resolved "https://registry.yarnpkg.com/source-map-js/-/source-map-js-1.2.1.tgz#1ce5650fddd87abc099eda37dcff024c2667ae46"
integrity sha512-UXWMKhLOwVKb728IUtQPXxfYU+usdybtUrK/8uGE8CQMvrhOpwvzDBwj0QhSL7MQc7vIsISBG8VQ8+IDQxpfQA==

source-map@0.8.0-beta.0:
version "0.8.0-beta.0"
Expand Down

0 comments on commit 760aff1

Please sign in to comment.