-
Notifications
You must be signed in to change notification settings - Fork 2.5k
fix: Code editor input error #4490
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
base: v2
Are you sure you want to change the base?
Conversation
|
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. |
|
[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 |
| const extensions = [python(), oneDark, regexpLinter] | ||
| const codemirrorStyle = { | ||
| height: '210px!important', | ||
| width: '100%', |
There was a problem hiding this comment.
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:
-
Consistent Throttle vs Debounce:
- The
asyncLintfunction is currently usingdebounce, but it should be set tothrottle. This ensures that the API call does not happen more frequently than every 500 milliseconds.
- The
-
Potential Off-by-One Error:
- In the
getRangeFromLineAndColumnfunction, there's logic to calculate the range positions considering end columns. However, this might lead to off-by-one errors ifcolumnorend_columnare invalid indices. You might want to add checks for these cases early.
- In the
-
Variable Names:
- There are some variable names that could be clearer or improved. For example,
safeColumnandfinalTocould be renamed for better readability.
- There are some variable names that could be clearer or improved. For example,
-
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.
fix: Code editor input error