Skip to content
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

Fix cobol extension and cleanup #112

Merged
merged 3 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading