-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: properly import cobol extension
- Loading branch information
Loïc Mangeonjean
committed
Apr 17, 2024
1 parent
0d6bdf3
commit 775b913
Showing
10 changed files
with
192 additions
and
106 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,69 @@ | ||
const vscode = require('vscode') | ||
|
||
const fixedPositions = [0, 6, 7, 11] | ||
const lastFixedPosition = fixedPositions[fixedPositions.length - 1] | ||
|
||
function findFirstNonSpacePosition (line) { | ||
for (let i = 0; i < line.length; i++) { | ||
if (line[i] !== ' ') { | ||
return i | ||
} | ||
} | ||
return line.length | ||
} | ||
|
||
async function indentEditor (editor) { | ||
await editor.edit(builder => { | ||
for (const selection of editor.selections) { | ||
for (let lineNumber = selection.start.line; lineNumber <= selection.end.line; lineNumber++) { | ||
const line = editor.document.lineAt(lineNumber).text | ||
const nonSpacePosition = findFirstNonSpacePosition(line) | ||
const nextFixedPosition = fixedPositions.find(p => p > nonSpacePosition) | ||
|
||
let expectedIndent = nextFixedPosition | ||
if (expectedIndent == null) { | ||
const indentWidth = editor.options.tabSize | ||
const expectedIndentCount = Math.floor((nonSpacePosition - lastFixedPosition) / indentWidth) + 1 | ||
expectedIndent = lastFixedPosition + (indentWidth) * expectedIndentCount | ||
} | ||
|
||
const toInsert = expectedIndent - nonSpacePosition | ||
|
||
builder.insert(new vscode.Position(lineNumber, 0), ' '.repeat(toInsert)) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
async function unindentEditor (editor) { | ||
await editor.edit(builder => { | ||
for (const selection of editor.selections) { | ||
for (let lineNumber = selection.start.line; lineNumber <= selection.end.line; lineNumber++) { | ||
const line = editor.document.lineAt(lineNumber).text | ||
const nonSpacePosition = findFirstNonSpacePosition(line) | ||
const fixedIndentWidth = editor.options.tabSize | ||
const prevFixedPosition = fixedPositions.slice().reverse().find(p => p < nonSpacePosition) ?? 0 | ||
|
||
let expectedIndent = prevFixedPosition | ||
|
||
if (prevFixedPosition === lastFixedPosition) { | ||
// Instead of going back to the last fixed position, go to the nearest (prevFixedPosition + tabSize * N) position | ||
const expectedIndentCount = Math.floor((nonSpacePosition - lastFixedPosition - 1) / fixedIndentWidth) | ||
expectedIndent = lastFixedPosition + fixedIndentWidth * expectedIndentCount | ||
} | ||
|
||
const toRemove = nonSpacePosition - expectedIndent | ||
|
||
builder.delete(new vscode.Range( | ||
lineNumber, | ||
0, | ||
lineNumber, | ||
0 + toRemove | ||
)) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
vscode.commands.registerTextEditorCommand('cobol-indent', indentEditor) | ||
vscode.commands.registerTextEditorCommand('cobol-unindent', unindentEditor) |
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,34 @@ | ||
import { ExtensionHostKind, registerExtension } from 'vscode/extensions' | ||
|
||
const { registerFileUrl } = registerExtension({ | ||
name: 'cobol-indent', | ||
publisher: 'codingame', | ||
version: '1.0.0', | ||
engines: { | ||
vscode: '*' | ||
}, | ||
activationEvents: ['onLanguage:cobol'], | ||
contributes: { | ||
commands: [{ | ||
command: 'cobol-indent', | ||
title: 'Indent cobol', | ||
enablement: 'editorLangId == cobol && !inSnippetMode' | ||
}, { | ||
command: 'cobol-unindent', | ||
title: 'Unindent cobol', | ||
enablement: 'editorLangId == cobol && !inSnippetMode' | ||
}], | ||
keybindings: [{ | ||
command: 'cobol-indent', | ||
key: 'tab', | ||
when: 'editorLangId == cobol && !inSnippetMode' | ||
}, { | ||
command: 'cobol-unindent', | ||
key: 'shift+tab', | ||
when: 'editorLangId == cobol && !inSnippetMode' | ||
}] | ||
}, | ||
browser: './extension.js' | ||
}, ExtensionHostKind.LocalWebWorker) | ||
|
||
registerFileUrl('./extension.js', new URL('./cobol-extension.js', import.meta.url).href) |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
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
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