Skip to content

Conversation

@shaohuzhang1
Copy link
Contributor

fix: Code editor input error

@f2c-ci-robot
Copy link

f2c-ci-robot bot commented Dec 10, 2025

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@f2c-ci-robot
Copy link

f2c-ci-robot bot commented Dec 10, 2025

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

const extensions = [python(), oneDark, regexpLinter]
const codemirrorStyle = {
height: '210px!important',
width: '100%',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a couple of potential optimizations and corrections in the code:

  1. Consistent Throttle vs Debounce:

    • The asyncLint function is currently using debounce, but it should be set to throttle. This ensures that the API call does not happen more frequently than every 500 milliseconds.
  2. Potential Off-by-One Error:

    • In the getRangeFromLineAndColumn function, there's logic to calculate the range positions considering end columns. However, this might lead to off-by-one errors if column or end_column are invalid indices. You might want to add checks for these cases early.
  3. Variable Names:

    • There are some variable names that could be clearer or improved. For example, safeColumn and finalTo could be renamed for better readability.
  4. Code Formatting:

    • Ensure consistent indentation and spacing throughout the file for better readability.

Here's the corrected version of the code with these suggestions applied:

// ... other imports ...

function getRangeFromLineAndColumn(state: any, line: number, column: number, end_column?: number) {
  const l = state.doc.line(line);
  const safeColumn = Math.max(0, Math.min(column, l.length));
  const fromPos = l.from + safeColumn;
  
  let safeEndColumn;
  if (end_column !== undefined) {
    safeEndColumn = Math.max(0, Math.min(end_column, l.length));
  } else {
    safeEndColumn = l.length;
  }

  const toPos = l.from + safeEndColumn;

  return {
    from: Math.min(fromPos, toPos),
    to: Math.max(fromPos, toPos),
  };
}

const asyncLintThrottled = throttle(async (view: any) => {
  const res = await loadSharedApi({ type: 'tool', systemType: apiType.value }).postPylint(view.state.doc.toString());
  return res.data;
}, 500);

const regexpLinter = linter(async (view) => {
  const currentstate = view.state;
  const diagnostics: Diagnostic[] = [];
  const lintResults = await asyncLintThrottled(view); // Use the throttled function for consistency

  if (!lintResults || lintResults.length === 0) {
    return diagnostics; // Return diagnostics only if they are available
  }

  const limitedResults = lintResults.filter((result) => result.severity === 'info' || result.severity === 'warning'); // Limit results based on severity

  limitedResults.forEach((element: any) => {
    try {
      const range = getRangeFromLineAndColumn(currentstate, element.line, element.column, element.endColumn);
      
      // Add validation here if necessary...
      if (range.from >= 0 && range.to >= range.from){
        diagnostics.push({
          from: range.from,
          to: range.to,
          severity: element.type === 'error' ? 'error' : 'warning',
          message: element.message,
        });
      }
    } catch {};
  });

  return diagnostics;
});

const extensions = [
  python(),
  onesDark,
  regexpLinter
];

const codemirrorStyle = {
  height: "210px!important",
  width: "100%"
};

These changes ensure that the code functions correctly and improves its maintainability by being clear about intent and handling edge cases appropriately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants