Skip to content

Commit

Permalink
fix: transform cobol feature into VSCode extension
Browse files Browse the repository at this point in the history
  • Loading branch information
Loïc Mangeonjean committed Apr 17, 2024
1 parent 1e40131 commit c589119
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 105 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ dist/*
stats.html
.vscode
node_modules
extensions
/extensions
90 changes: 0 additions & 90 deletions src/editor/cobol.ts

This file was deleted.

8 changes: 0 additions & 8 deletions src/editor/index.ts

This file was deleted.

101 changes: 101 additions & 0 deletions src/extensions/cobol.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { ExtensionHostKind, registerExtension } from 'vscode/extensions'
import type * as vscode from 'vscode'

const fixedPositions = [0, 6, 7, 11]
const lastFixedPosition = fixedPositions[fixedPositions.length - 1]!

function findFirstNonSpacePosition (line: string): number {
for (let i = 0; i < line.length; i++) {
if (line[i] !== ' ') {
return i
}
}
return line.length
}

const { getApi } = registerExtension({
name: 'cobol-indent',
publisher: 'codingame',
version: '1.0.0',
engines: {
vscode: '*'
},
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'
}]
}
}, ExtensionHostKind.LocalProcess)

void getApi().then(api => {
async function indentEditor (editor: vscode.TextEditor) {
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 as number
const expectedIndentCount = Math.floor((nonSpacePosition - lastFixedPosition) / indentWidth) + 1
expectedIndent = lastFixedPosition + (indentWidth) * expectedIndentCount
}

const toInsert = expectedIndent - nonSpacePosition

builder.insert(new api.Position(lineNumber, 0), ' '.repeat(toInsert))
}
}
})
}

async function unindentEditor (editor: vscode.TextEditor) {
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 as number
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 api.Range(
lineNumber,
0,
lineNumber,
0 + toRemove
))
}
}
})
}

api.commands.registerTextEditorCommand('cobol-indent', indentEditor)
api.commands.registerTextEditorCommand('cobol-unindent', unindentEditor)
})
1 change: 1 addition & 0 deletions src/extensions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './cobol'
8 changes: 2 additions & 6 deletions src/monaco.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { IReference, ITextFileEditorModel, createConfiguredEditor, errorHandler,
import { editorOpenHandlerRegistry, initializePromise, isInitialized } from './services'
import './languages'
import './worker'
import setupExtensions from './editor'
import './extensions'
import { EditorOpenHandler } from './tools/EditorOpenHandlerRegistry'

errorHandler.setUnexpectedErrorHandler(error => {
Expand All @@ -14,11 +14,7 @@ function createEditor (domElement: HTMLElement, options?: monaco.editor.IStandal
if (!isInitialized()) {
throw new Error('Monaco not initialized')
}
const editor = createConfiguredEditor(domElement, options)

setupExtensions(editor)

return editor
return createConfiguredEditor(domElement, options)
}

async function createModelReference (resource: monaco.Uri, content?: string): Promise<IReference<ITextFileEditorModel>> {
Expand Down

0 comments on commit c589119

Please sign in to comment.