Skip to content

Commit

Permalink
Merge pull request #112 from CodinGame/cleanup
Browse files Browse the repository at this point in the history
Fix cobol extension and cleanup
  • Loading branch information
CGNonofr authored Apr 17, 2024
2 parents 5f85ba4 + c589119 commit 0d6bdf3
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 151 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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ Monaco editor wrapper that adds some features and improvements to it:
- It configures the workers
- It adds some features:
- Smart tabs in Cobol
- `editor.foldAllAutofoldRegions` action
- A way to register a text model content provider and a editor open handler
- It allows the opening of an overlay editor when navigating to an external file
- It adds some language aliases
Expand Down
23 changes: 0 additions & 23 deletions src/editor/autofold.ts

This file was deleted.

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'
9 changes: 2 additions & 7 deletions src/monaco.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import * as monaco from 'monaco-editor'
import { IReference, ITextFileEditorModel, createConfiguredEditor, errorHandler, createModelReference as vscodeCreateModelReference } from 'vscode/monaco'
import { editorOpenHandlerRegistry, initializePromise, isInitialized } from './services'
import './languages'
import './theme'
import './worker'
import setupExtensions from './editor'
import './extensions'
import { EditorOpenHandler } from './tools/EditorOpenHandlerRegistry'

errorHandler.setUnexpectedErrorHandler(error => {
Expand All @@ -15,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
21 changes: 0 additions & 21 deletions src/theme/index.ts

This file was deleted.

0 comments on commit 0d6bdf3

Please sign in to comment.