Skip to content

Commit

Permalink
fix: properly import cobol extension
Browse files Browse the repository at this point in the history
  • Loading branch information
Loïc Mangeonjean committed Apr 17, 2024
1 parent 0d6bdf3 commit 775b913
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 106 deletions.
69 changes: 69 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@
"@typescript-eslint/eslint-plugin": "7.6.0",
"@typescript-eslint/parser": "7.6.0",
"@vscode/vsce": "^2.25.0",
"@web/rollup-plugin-import-meta-assets": "^2.2.1",
"azure-devops-node-api": "^13.0.0",
"conventional-changelog-conventionalcommits": "^7.0.2",
"cson-parser": "4.0.9",
Expand Down
14 changes: 14 additions & 0 deletions rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as rollup from 'rollup'
import vsixPlugin from '@codingame/monaco-vscode-rollup-vsix-plugin'
import glob from 'fast-glob'
import { addExtension } from '@rollup/pluginutils'
import { importMetaAssets } from '@web/rollup-plugin-import-meta-assets'
import path from 'path'
import pkg from './package.json' assert { type: 'json' }

Expand Down Expand Up @@ -42,6 +43,19 @@ export default rollup.defineConfig({
chunkFileNames: '[name].js'
}],
plugins: [
importMetaAssets({
include: ['**/customExtensions/*.ts']
}),
{
name: 'resolve-asset-url',
resolveFileUrl (options) {
let relativePath = options.relativePath
if (!relativePath.startsWith('.')) {
relativePath = `./${options.relativePath}`
}
return `'${relativePath}'`
}
},
{
name: 'external-resolver',
resolveId (id) {
Expand Down
69 changes: 69 additions & 0 deletions src/customExtensions/cobol-extension.js
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)
34 changes: 34 additions & 0 deletions src/customExtensions/cobol.ts
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.
101 changes: 0 additions & 101 deletions src/extensions/cobol.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/languages/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as monaco from 'monaco-editor'
import { StandaloneServices, ILanguageService } from 'vscode/services'
import { StandaloneServices, ILanguageService, getService } from 'vscode/services'

const customAliases: Partial<Record<string, string[]>> = {
csharp: ['c#'],
Expand Down Expand Up @@ -38,7 +38,7 @@ function getMonacoLanguage (languageOrModeId: string): string {
}

async function loadLanguage (languageId: string): Promise<void> {
StandaloneServices.get(ILanguageService).createById(languageId)
(await getService(ILanguageService)).createById(languageId)
}

export {
Expand Down
3 changes: 0 additions & 3 deletions src/monaco.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
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 './worker'
import './extensions'
import { EditorOpenHandler } from './tools/EditorOpenHandlerRegistry'

errorHandler.setUnexpectedErrorHandler(error => {
Expand Down
3 changes: 3 additions & 0 deletions src/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import { IWorkbenchConstructionOptions, IWorkspaceProvider } from 'vscode/vscode
import EditorOpenHandlerRegistry from './tools/EditorOpenHandlerRegistry'
import { whenReady as whenExtensionsReady } from './extensions'
import 'vscode/localExtensionHost'
import './customExtensions'
import './languages'
import './worker'

const defaultFilesystemProvider = new RegisteredFileSystemProvider(false)
registerFileSystemOverlay(1, defaultFilesystemProvider)
Expand Down

0 comments on commit 775b913

Please sign in to comment.