-
Notifications
You must be signed in to change notification settings - Fork 234
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
[Not Ready] [typespec-vscode] Typespec vscode localization #5466
Open
mzhongl524
wants to merge
6
commits into
microsoft:main
Choose a base branch
from
mzhongl524:typespec-vscode-localization
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b0ac596
Merge pull request #1 from microsoft/main
mzhongl524 3fcbe52
Merge branch 'microsoft:main' into main
mzhongl524 c109816
Merge branch 'microsoft:main' into main
mzhongl524 23f0108
Merge branch 'microsoft:main' into main
mzhongl524 a8e5aec
add codefix for `triple-quote-indent` warning ,and meanwhile add test…
mzhongl524 98811ab
Initialize localization
mzhongl524 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
152 changes: 152 additions & 0 deletions
152
packages/compiler/src/core/compiler-code-fixes/triple-quote-indent.codefix.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
import { isLineBreak, isWhiteSpaceSingleLine } from "../charcode.js"; | ||
import { defineCodeFix, getSourceLocation } from "../diagnostics.js"; | ||
import type { CodeFixEdit, DiagnosticTarget, SourceLocation } from "../types.js"; | ||
|
||
export function createTripleQuoteIndentCodeFix(diagnosticTarget: DiagnosticTarget) { | ||
return defineCodeFix({ | ||
id: "triple-quote-indent", | ||
label: "Format triple-quote-indent", | ||
fix: (context) => { | ||
const result: CodeFixEdit[] = []; | ||
|
||
const location = getSourceLocation(diagnosticTarget); | ||
const { startPos: startPosArr, indent } = findStartPositionAndIndent(location); | ||
startPosArr.map((pos) => { | ||
const updatedLocation = { ...location, pos }; | ||
result.push(context.prependText(updatedLocation, indent)); | ||
}); | ||
|
||
return result; | ||
}, | ||
}); | ||
} | ||
|
||
function isNoNewlineStartTripleQuote(start: number, end: number, input: string): boolean { | ||
while (start < end && isWhiteSpaceSingleLine(input.charCodeAt(start))) { | ||
start++; | ||
} | ||
return !isLineBreak(input.charCodeAt(start)); | ||
} | ||
|
||
function isNoNewlineEndTripleQuote(start: number, end: number, input: string): boolean { | ||
while (end > start && isWhiteSpaceSingleLine(input.charCodeAt(end - 1))) { | ||
end--; | ||
} | ||
return !isLineBreak(input.charCodeAt(end - 1)); | ||
} | ||
|
||
function getSpaceNumbBetweenStartPosAndVal(start: number, end: number, input: string): number { | ||
while (start < end && isWhiteSpaceSingleLine(input.charCodeAt(start))) { | ||
start++; | ||
} | ||
if (isLineBreak(input.charCodeAt(start))) { | ||
start += 2; | ||
} | ||
|
||
let spaceNumb = 0; | ||
while (start < end && isWhiteSpaceSingleLine(input.charCodeAt(start))) { | ||
spaceNumb++; | ||
start++; | ||
} | ||
return spaceNumb; | ||
} | ||
|
||
function getSpaceNumbBetweenEnterAndEndPos(start: number, end: number, input: string): number { | ||
let spaceNumb = 0; | ||
while (end > start && isWhiteSpaceSingleLine(input.charCodeAt(end - 1))) { | ||
spaceNumb++; | ||
end--; | ||
} | ||
return spaceNumb; | ||
} | ||
|
||
function findStartPositionAndIndent(location: SourceLocation): { | ||
startPos: number[]; | ||
indent: string; | ||
} { | ||
const text = location.file.text; | ||
const splitOrIndentStr = "\r\n"; | ||
const startPos = location.pos; | ||
const endPos = location.end; | ||
const offSet = 3; // The length of `"""` | ||
|
||
const noNewlineStart = isNoNewlineStartTripleQuote(startPos + offSet, endPos, text); | ||
const noNewlineEnd = isNoNewlineEndTripleQuote(startPos, endPos - offSet, text); | ||
if (noNewlineStart && noNewlineEnd) { | ||
// eg. `""" one two """` | ||
return { startPos: [startPos + offSet, endPos - offSet], indent: splitOrIndentStr }; | ||
} else if (noNewlineStart) { | ||
// eg. `""" one two \r\n"""` | ||
const startSpaceNumb = getSpaceNumbBetweenStartPosAndVal(startPos + offSet, endPos, text); | ||
const endSpaceNumb = getSpaceNumbBetweenEnterAndEndPos(startPos, endPos - offSet, text); | ||
|
||
// Only in the case of equals, the `triple-quote-indent` warning will be triggered. | ||
// The `no-new-line-start-triple-quote` warning is triggered when it is greater than | ||
if (startSpaceNumb >= endSpaceNumb) { | ||
return { startPos: [startPos + offSet], indent: splitOrIndentStr }; | ||
} else { | ||
return { | ||
startPos: [startPos + offSet], | ||
indent: splitOrIndentStr + " ".repeat(endSpaceNumb - startSpaceNumb), | ||
}; | ||
} | ||
} else if (noNewlineEnd) { | ||
// eg. `"""\r\n one two """` | ||
const startSpaceNumb = getSpaceNumbBetweenStartPosAndVal(startPos + offSet, endPos, text); | ||
const endSpaceNumb = getSpaceNumbBetweenEnterAndEndPos(startPos, endPos - offSet, text); | ||
if (startSpaceNumb < endSpaceNumb) { | ||
return { | ||
startPos: [endPos - offSet], | ||
indent: splitOrIndentStr + " ".repeat(startSpaceNumb), | ||
}; | ||
} else { | ||
// Detailed description: `no-new-line-start-triple-quote`, `no-new-line-end-triple-quote` | ||
// and `triple-quote-indent` are all warnings about incorrect triple quote values. | ||
// Currently, only `triple-quote-indent` has a quick fix. | ||
// Todo: add codefix for `no-new-line-start-triple-quote` and `no-new-line-end-triple-quote` warning | ||
|
||
// It will only warn that the ending """ is not on a new line and will not trigger the triple quote indent warning. | ||
return { | ||
startPos: [endPos - offSet], | ||
indent: splitOrIndentStr + " ".repeat(startSpaceNumb - endSpaceNumb), | ||
}; | ||
} | ||
} else { | ||
// eg. `"""\r\none\r\n two\r\n """` | ||
const endSpaceNumb = getSpaceNumbBetweenEnterAndEndPos(startPos, endPos - offSet, text); | ||
const arrIndents: number[] = []; | ||
let start = startPos + offSet; | ||
|
||
// Calculate the number of spaces needed to align each line | ||
while (start > 0 && start < endPos) { | ||
const currLineSpaceNumb = getSpaceNumbBetweenStartPosAndVal(start, endPos, text); | ||
arrIndents.push(currLineSpaceNumb); | ||
|
||
// If it is 0, the method indexOf cannot get the next `\r\n` position, so add 1 | ||
start += currLineSpaceNumb === 0 ? 1 : currLineSpaceNumb; | ||
start = text.indexOf(splitOrIndentStr, start); | ||
} | ||
|
||
// Find all the positions of `\r\n` and remove the last one because it is the position of `"""` | ||
const arrStartPos: number[] = []; | ||
start = startPos + offSet; | ||
while (start < endPos) { | ||
start = text.indexOf(splitOrIndentStr, start); | ||
if (start < 0) { | ||
break; | ||
} | ||
start += 2; | ||
if (start < endPos) { | ||
arrStartPos.push(start); | ||
} | ||
} | ||
arrStartPos.pop(); | ||
|
||
//If minSpaceNumb is larger than endSpaceNumb, codefix will not be generated | ||
const minSpaceNumb = Math.min(...arrIndents); | ||
return { | ||
startPos: arrStartPos, | ||
indent: " ".repeat(endSpaceNumb - minSpaceNumb), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
packages/compiler/test/core/compiler-code-fixes/triple-quote-indent.codefix.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { strictEqual } from "assert"; | ||
import { describe, it } from "vitest"; | ||
import { createTripleQuoteIndentCodeFix } from "../../../src/core/compiler-code-fixes/triple-quote-indent.codefix.js"; | ||
import { SyntaxKind } from "../../../src/index.js"; | ||
import { expectCodeFixOnAst } from "../../../src/testing/code-fix-testing.js"; | ||
|
||
describe("CodeFix: triple-quote-indent", () => { | ||
it("case 1: each triple-quote is on a new line", async () => { | ||
await expectCodeFixOnAst( | ||
` | ||
const a = ┆"""\r\none\r\n two\r\n """; | ||
`, | ||
(node) => { | ||
strictEqual(node.kind, SyntaxKind.StringLiteral); | ||
return createTripleQuoteIndentCodeFix(node); | ||
}, | ||
).toChangeTo(` | ||
const a = """\r\n one\r\n two\r\n """; | ||
`); | ||
}); | ||
|
||
it("case 2: all triple-quote is on one line", async () => { | ||
await expectCodeFixOnAst( | ||
` | ||
const a = ┆""" one\r\n two """; | ||
`, | ||
(node) => { | ||
strictEqual(node.kind, SyntaxKind.StringLiteral); | ||
return createTripleQuoteIndentCodeFix(node); | ||
}, | ||
).toChangeTo(` | ||
const a = """\r\n one\r\n two \r\n"""; | ||
`); | ||
}); | ||
|
||
it("case 3: start triple-quote is not on a new line but end one is", async () => { | ||
await expectCodeFixOnAst( | ||
` | ||
const a = ┆"""one\r\n two\r\n """; | ||
`, | ||
(node) => { | ||
strictEqual(node.kind, SyntaxKind.StringLiteral); | ||
return createTripleQuoteIndentCodeFix(node); | ||
}, | ||
).toChangeTo(` | ||
const a = """\r\n one\r\n two\r\n """; | ||
`); | ||
}); | ||
|
||
it("case 4: end triple-quote is not on a new line but start one is", async () => { | ||
await expectCodeFixOnAst( | ||
` | ||
const a = ┆"""\r\n one\r\n two """; | ||
`, | ||
(node) => { | ||
strictEqual(node.kind, SyntaxKind.StringLiteral); | ||
return createTripleQuoteIndentCodeFix(node); | ||
}, | ||
).toChangeTo(` | ||
const a = """\r\n one\r\n two \r\n """; | ||
`); | ||
}); | ||
}); |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a little reluctant to add any translation for now as the compiler is not supporting it and this will just provide this very partially translated experience |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"Launching TypeSpec language service...": "Launching TypeSpec language service...", | ||
"See documentation for \"{0}\"": "See documentation for \"{0}\"" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"typespec.restartServer.title": "Restart TypeSpec server", | ||
"typespec.showOutputChannel.title": "Show Output Channel" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
this doesn't seem to belong to this pr?