Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: correct position of monaco decorations (#15787)

h3n4l4a44ed07ea9d
diff --git a/frontend/src/components/MonacoEditor/composables/useAdvices.ts b/frontend/src/components/MonacoEditor/composables/useAdvices.ts
index 2b37bf417f..ed112a5ce7 100644
--- a/frontend/src/components/MonacoEditor/composables/useAdvices.ts
+++ b/frontend/src/components/MonacoEditor/composables/useAdvices.ts
@@ -3,6 +3,7 @@ import type monaco from "monaco-editor";
import { unref, watchEffect } from "vue";
import type { MaybeRef } from "@/types";
import { callCssVariable, escapeMarkdown } from "@/utils";
+import { batchConvertPositionToMonacoPosition } from "@/utils/v1/position";
import type { AdviceOption, MonacoModule } from "../types";
export const useAdvices = (
@@ -17,14 +18,36 @@ export const useAdvices = (
_advices.map((m) => m.severity),
(s) => levelOfSeverity(s)
) ?? "WARNING";
+ const content = editor.getModel()?.getValue() ?? "";
+ const protoStartPositions = _advices.map((advice) => {
+ return {
+ line: advice.startLineNumber,
+ column: advice.startColumn,
+ };
+ });
+ const protoEndPositions = _advices.map((advice) => {
+ return {
+ line: advice.endLineNumber,
+ column: advice.endColumn,
+ };
+ });
+ const monacoStartPosition = batchConvertPositionToMonacoPosition(
+ protoStartPositions,
+ content
+ );
+ const monacoEndPosition = batchConvertPositionToMonacoPosition(
+ protoEndPositions,
+ content
+ );
+
const decorators = editor.createDecorationsCollection(
- _advices.map((advice) => {
+ _advices.map((advice, index) => {
return {
range: new monaco.Range(
- advice.startLineNumber,
- advice.startColumn,
- advice.endLineNumber,
- advice.endColumn
+ monacoStartPosition[index].lineNumber,
+ monacoStartPosition[index].column,
+ monacoEndPosition[index].lineNumber,
+ monacoEndPosition[index].column
),
options: {
className:
diff --git a/frontend/src/components/PlanCheckRun/PlanCheckRunDetail.vue b/frontend/src/components/PlanCheckRun/PlanCheckRunDetail.vue
index 52e6ebc1f9..3b2e0f70ea 100644
--- a/frontend/src/components/PlanCheckRun/PlanCheckRunDetail.vue
+++ b/frontend/src/components/PlanCheckRun/PlanCheckRunDetail.vue
@@ -102,12 +102,17 @@
<span
class="normal-link"
@click="
- handleClickPlanCheckDetailLine(
+ convertPositionLineToMonacoLine(
row.checkResult.sqlReviewReport!.line
)
"
>
- Line {{ row.checkResult.sqlReviewReport.line }}
+ Line
+ {{
+ convertPositionLineToMonacoLine(
+ row.checkResult.sqlReviewReport.line
+ )
+ }}
</span>
</div>
</div>
@@ -186,11 +191,18 @@
class="ml-1 normal-link"
@click="
handleClickPlanCheckDetailLine(
- row.checkResult.sqlReviewReport!.line
+ convertPositionLineToMonacoLine(
+ row.checkResult.sqlReviewReport!.line
+ )
)
"
>
- Line {{ row.checkResult.sqlReviewReport.line }}
+ Line
+ {{
+ convertPositionLineToMonacoLine(
+ row.checkResult.sqlReviewReport.line
+ )
+ }}
</span>
</template>
</div>
@@ -252,6 +264,7 @@ import {
PlanCheckRun_Result_Status,
PlanCheckRun_Status,
} from "@/types/proto/v1/plan_service";
+import { convertPositionLineToMonacoLine } from "@/utils/v1/position";
import { usePlanCheckRunContext } from "./context";
import { OnlineMigrationDetail } from "./detail";
diff --git a/frontend/src/utils/v1/position.ts b/frontend/src/utils/v1/position.ts
new file mode 100644
index 0000000000..9f6275b763
--- /dev/null
+++ b/frontend/src/utils/v1/position.ts
@@ -0,0 +1,62 @@
+import type * as monaco from "monaco-editor/esm/vs/editor/editor.api.js";
+import type { Position } from "@/types/proto/v1/common";
+
+export function batchConvertPositionToMonacoPosition(
+ positions: Position[],
+ text: string,
+ _text_encoding: string = "utf-8",
+ _position_encoding: string = "utf-16"
+): monaco.IPosition[] {
+ const lines = text.split("\n");
+ const result: monaco.IPosition[] = [];
+ for (const position of positions) {
+ if (position.line < 0) {
+ result.push({
+ lineNumber: 1,
+ column: 1,
+ });
+ continue;
+ }
+ // Assuming the text is in utf-8 encoding.
+ const lineNumber = position.line + 1;
+ if (lineNumber > lines.length) {
+ // Out of bounds, return the first character of the last line.
+ result.push({
+ lineNumber: lines.length,
+ column: 1,
+ });
+ continue;
+ }
+
+ const line = lines[lineNumber - 1];
+ let column = 1;
+ for (let i = 0; i < line.length; i++) {
+ if (position.column <= i) {
+ result.push({
+ lineNumber: lineNumber,
+ column: column,
+ });
+ break;
+ }
+ const codePoint = line.codePointAt(i);
+ if (codePoint === undefined) {
+ break;
+ }
+ const codeUnitCount = codePoint > 0xffff ? 2 : 1;
+ column += codeUnitCount;
+ }
+ result.push({
+ lineNumber: lineNumber,
+ column: column,
+ });
+ }
+
+ return result;
+}
+
+export function convertPositionLineToMonacoLine(line: number) {
+ if (line < 0) {
+ return 1;
+ }
+ return line + 1;
+}