Skip to content

Commit

Permalink
feat(ui): Add simple language detection for code browser (#2780)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtruong authored Oct 29, 2024
1 parent 9841974 commit abbe2e0
Showing 1 changed file with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ import React, {FC} from 'react';
import {Alert} from '../../../Alert';
import {useWFHooks} from '../Browse3/pages/wfReactInterface/context';

function detectLanguage(uri: string, code: string) {
// Simple language detection based on file extension or content
if (uri.endsWith('.py')) {
return 'python';
}
if (uri.endsWith('.js') || uri.endsWith('.ts')) {
return 'javascript';
}
if (code.includes('def ') || code.includes('import ')) {
return 'python';
}
if (code.includes('function ') || code.includes('const ')) {
return 'javascript';
}
return 'plaintext';
}

export const Browse2OpDefCode: FC<{uri: string; maxRowsInView?: number}> = ({
uri,
maxRowsInView,
Expand Down Expand Up @@ -37,10 +54,12 @@ export const Browse2OpDefCode: FC<{uri: string; maxRowsInView?: number}> = ({
);
}

const detectedLanguage = detectLanguage(uri, text.result ?? '');

const inner = (
<Editor
height={'100%'}
defaultLanguage="python"
defaultLanguage={detectedLanguage}
loading={text.loading}
value={text.result ?? ''}
options={{
Expand Down

0 comments on commit abbe2e0

Please sign in to comment.